您需要进行 AJAX 调用。以下是解释代码
ajax_call.php
<html>
<head>
<script type="text/javascript">
//function which is called as soon as a user types a single character
function update()
{
//Step 1:create XMLHttpRequest object to make AJAX call.
try{
//for firefox,chrome and opera.
xmlHttp=new XMLHttpRequest();
}catch(e){
try{
//for IE
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e2){
//Otherwise, notify browser doesn't support ajax.
alert('Sorry ! AJAX not supported');
}
}
//create a handler function to handle the response
xmlHttp.onreadystatechange=function(){
//execute the code only when response is successfull
//readyState=4 denotes success
//HTTP status=200 denotes OK.
if(xmlHttp.readyState==4&&xmlHttp.status==200){
//update the div inside HTML with the respone text received.
document.getElementById('content').innerHTML=xmlHttp.responseText;
}
}
//make AJAX call
xmlHttp.open('GET','ajax_reply.php?content='+document.getElementById('search_text').value,true);
xmlHttp.send(null);
}
</script>
</head>
<body>
<form name='myForm'>
<input type="text" id="search_text" onkeyup="update()">
</form>
<div id="content"></div>
</body>
</html>
现在ajax_reply.php
,您可以设置变量或回复响应或做任何您喜欢的事情的代码。
<?php
if(isset($_GET['content']))
echo $_GET['content'];
//you can set the variable here like:-
//$text = $_GET['content'];
?>
我希望这有帮助。