.net开发人员试图为朋友做一个php站点,到目前为止一切都很顺利,但我想知道php是否有类似textchanged事件的东西。以下是我想做的,我想要一个下拉框,根据用户在上面的文本框中输入的内容,从数据库中检索到的数据追加到下拉框(使用文本框中的文本作为参数从数据库中检索数据,并将其追加到下拉框中,而无需重新加载整个页面)
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
//Do stuff
}
上面的block off代码是在asp.net中,但我想在php中实现类似的东西。
这不是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>
";
}
?>
解释:
java脚本,首先,我们包含了在线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 (web上的许多大玩家使用jquery),如果你知道一点java脚本的语法可能会令人不安。
在jquery中,我们有很多可以直接使用的方法,比如:$.post();
如果您想使用从该函数返回的数据,我们创建一个回调函数,如:
$.post( function(param_ returned_by_parent_function){
//do stuf
});
另一种使用jquery的方法,这实际上是它背后的想法是查询一个html元素,然后用它做这样的事情。
$('html_element_query').do_something_with_this();
当然这只是一个基本的解释但是也许你已经明白了
可以使用javascript的onChange处理程序,通过AJAX将当前值发送给php
https://developer.mozilla.org/en/docs/DOM/element.onchange
http://www.w3schools.com/ajax/
PHP不知道客户端发生了什么。如果你想让客户端上的一些事件触发操作,你必须自己编写代码(通常用JavaScript)。
PHP本身无法感知前端发生的事件。但是,您可以通过混合使用Ajax和PHP来插入功能(某种程度上)。Ajax将监视事件,PHP将处理从该Ajax发送给它的数据。
我建议使用jQuery并查看http://api.jquery.com/Ajax_Events/
我为自己做了一个非常简单的PHP事件调度程序,它是可测试的,并已在我的网站上使用。如果你需要的话,可以看一下