PHP事件处理程序
.net开发人员正在尝试为朋友创建一个php站点,到目前为止一切都很顺利,但我想知道php是否有类似textchanged事件的功能。这就是我想要做的,我想要一个下拉框附加根据用户在上面文本框中输入的内容从数据库检索的数据(使用文本框中的文本作为参数从数据库检索数据,并将其附加到下拉框中,而无需重新加载整个页面)PHP事件处理程序,php,html,Php,Html,.net开发人员正在尝试为朋友创建一个php站点,到目前为止一切都很顺利,但我想知道php是否有类似textchanged事件的功能。这就是我想要做的,我想要一个下拉框附加根据用户在上面文本框中输入的内容从数据库检索的数据(使用文本框中的文本作为参数从数据库检索数据,并将其附加到下拉框中,而无需重新加载整个页面) 上面的代码块在asp.net中,但我想在php中实现类似的功能。您可以使用javascript onChange处理程序,并通过AJAX将当前值发送到php PHP不知道客户端上发生
上面的代码块在asp.net中,但我想在php中实现类似的功能。您可以使用javascript onChange处理程序,并通过AJAX将当前值发送到php
PHP不知道客户端上发生了什么。如果希望客户端上的某些事件触发操作,则必须自己编写代码(通常使用JavaScript)。PHP本身不知道前端发生的事件。但是,您可以通过混合使用Ajax和PHP来插入该功能(某种程度上)。Ajax将监视事件,PHP将处理从该Ajax发送给它的数据
我建议使用jQuery并签出这不是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>
";
}
?>
另一种使用jquery的方法,这实际上是它背后的想法,就是查询一个html元素,然后像这样处理它
$('html_element_query').do_something_with_this();
当然,这只是一个基本的解释,但也许你明白了。我为自己做了一个非常简单的解释,它是可测试的,并且已经在我的网站上使用过。如果您需要,可以查看。我认为普通PhP中没有事件,但使用Zend framework或Symfony之类的框架,您可能会发现此类事件。。。NEt中的事件只是普通HTTP请求的框架实现,结合了viewstate…PHP,在将任何内容传递给客户机/用户之前执行。因此,为了更新实时数据,您应该了解javascript-AJAX(jQuery是一个经过良好测试的具有AJAX特性的库)。只需使用JS监听更改,然后将数据传递给服务器PHP脚本,该脚本将生成新内容。我相信,如果不使用JS/AJAX,您不可能轻松完成这一任务。您需要编写一个PHP函数,从数据库中检索您需要的任何内容,在JS事件中触发它,然后使用JS异步地使用PHP函数的返回更新下拉列表。jQuery使AJAX变得非常简单,因此我建议您使用route.ASP.net的框架,当您编写这样的代码时,它可以构建客户端Javascript代码。PHP作为一种纯服务器端语言;它没有那种内置的框架;如果您想在PHP中实现这种功能,就需要找到一个提供这种功能的框架。有许多PHP框架;他们中的一个或多个可以做你想做的事。没错!不过,对AJAX进行更多的解释会更好。
<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>
$.post( function(param_ returned_by_parent_function){
//do stuf
});
$('html_element_query').do_something_with_this();