PHP事件处理程序

3

一位 .NET 开发者正在为朋友做一个 PHP 站点,目前一切进展顺利,但我想知道 PHP 是否有类似于 textchanged 事件的东西。这是我的想法,我想让一个下拉框根据用户在上方文本框中输入的内容从数据库中检索数据并追加到下拉框中(使用文本框中的文本作为参数从数据库中检索数据并将其追加到下拉框中而不重新加载整个页面)。

  protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        //Do stuff
    }

上面的代码块是asp.net的,但我想在php中实现类似的功能。


我认为在普通的PHP中并没有事件这种东西,但是如果使用像Zend框架或Symfony这样的框架,你可能会发现这种事件...在.NET中的事件只是基于HTTP请求和视图状态的框架实现。 - Laurent S.
1
PHP在将任何内容传递给客户端/用户之前就已经执行了。因此,为了更新实时数据,您应该查看JavaScript AJAX(jQuery是一个具有AJAX功能的经过充分测试的库)。只需使用JS监听更改,然后将数据传递到服务器PHP脚本,生成新内容即可。 - Daniel
我不相信你可以轻松地完成这个任务而不使用JS / AJAX。您需要编写一个PHP函数,从数据库中检索所需内容,将其触发在JS事件上,然后使用JS异步更新下拉菜单以返回PHP函数的结果。jQuery使AJAX变得非常容易,因此我建议您选择这条路线。 - Ennui
1
ASP.net有一个框架,当你编写类似的代码时,它会构建客户端Javascript代码。PHP作为一种语言纯粹是服务器端的;它没有内置这种类型的框架;如果你想在PHP中获得这种功能,你需要找到一个提供它的框架。有许多PHP框架;其中一个或多个可能会满足你的需求。 - Spudley
5个回答

3

这不是php的工作方式。但是你可以使用jquery进行如下ajax调用:

<?php

    //array, object or db result you use to fill your dropdown
    $array = array('pipo', 'kees', 'klaas', 'klaas1', 'jan', 'meneerje', 'poep', 'hessel', 'kaas', 'ietsandersd', 'smit', 'cowoy', 'nog zo iets');

    //if we want to search we search and only return the new found options
    if(isset($_REQUEST['keyword'])){
        $new_array = array();
        foreach($array as $value){
            if(strpos($value, $_REQUEST['keyword']) !== false){
                $new_array[] = $value;
            }
        }
    }
    else{
        $new_array = $array;
    }

    $options = '';
    foreach($new_array as $key => $option){
        $options .= "<option value='$key'>$option</option>";  
    }
    $selectbox = "<select name='selectbox' id='drop_down'>$options</select>";

    if(isset($_REQUEST['keyword'])){
        echo $options;
    }
    else{
        // with the \ we escape the "
        echo "<html>
                <head>
                    <title>ajax selectbox</title>
                    <script src=\"http://code.jquery.com/jquery-latest.min.js\" type=\"text/javascript\"></script>
                    <script type=\"text/javascript\">
                        $(document).ready(function () {
                            $('body').on('keyup', '.search', function(){
                                 var data = $('.search').serialize();
                                 $.post('ajax_selectbox.php', data, function (data){   
                                    $('#drop_down').html(data);
                                });
                            });
                        });
                    </script>
                </head>
                <body>
                    <input type='text' name='keyword' class='search' />
                    $selectbox
                </body>
                </html>
             ";
    }

?>

说明:

JavaScript, 首先我们需要引入在线的jQuery库,也可以下载该库并从您自己的Web服务器进行引用。

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
    // first we wait unit the html page is loaded
    $(document).ready(function () {
        //then we wait for a keyup event in the element with class="search" we use the css sector . for classes like .search
        $('body').on('keyup', '.search', function(){
            //when we type inside the .search textbox we serialize the element like a form would do. this takes the name and the value and puts it in a array.
            var data = $('.search').serialize();
            // then we post with ajax back to our php file or an other php file. its you own decision. the data variable is the serialized data form .search
            $.post('ajax_selectbox.php', data, function (data){
                // at least we use a calback for when the ajax event has finnest and we use the jquery html function to put the new options inside the drobbox with id="drop_down". we use the css id selector # to select the select box.   
                $('#drop_down').html(data);
            });
        });
    });
</script>

请注意,我使用jQuery(许多大型网站也使用jQuery),如果您了解一些JavaScript,那么语法可能会令人困扰。
在jQuery中,我们有一组可以直接使用的方法,例如: $.post(); 如果您想要使用该函数返回的数据,我们需要创建一个回调函数:
$.post( function(param_ returned_by_parent_function){
    //do stuf
});

另一种使用jQuery的方法是查询HTML元素,然后执行相关操作,例如:

$('html_element_query').do_something_with_this();

当然,这只是基本的解释,但或许你已经理解了。

没错!不过,能再详细解释一下 AJAX 吗? - GolDRoger

1

1

PHP不知道客户端发生了什么。如果您希望客户端的某些事件触发操作,则必须自己编写代码(通常使用JavaScript)。


1
PHP本身无法感知前端发生的事件。但是,您可以通过使用Ajax和PHP的混合来插入功能。 Ajax将监视事件,而PHP将处理从该Ajax发送到它的数据。
我建议使用jQuery并查看http://api.jquery.com/Ajax_Events/

0

我为自己制作了一个非常简单的PHP事件分发器,它是可测试的,并已在我的网站上使用。如果您需要,可以看一下。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接