我有一个像这样的html文件:
<?
$q=$_REQUEST['???'];//sth. like that
echo $q;
?>
<form>
<select name="users" onchange="showUser()">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
我想在php块的同一页面中使用onchange值选择。
我只想选择一些选项并在PHP中保存一个变量,就像我指示的那样。我怎么能在同一页面上做到这一点?
答案 0 :(得分:0)
你需要在javascript中有一个能处理AJAX请求的函数
你的onchange=""
函数调用的参数为this.value
,这意味着它会将选择框中选择的值带到javascript函数
在javascript函数showUser中,它将创建一个xmlhttp对象来执行ajax方法“open()
”和“send()
”
像:
if (window.XMLHttpRequest)
{ //For Other browsers
xmlhttp=new XMLHttpRequest();
}
else
{ //For internet Explorer
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
现在创建了一个函数,需要在响应准备就绪时执行
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("yourcontainerID").innerHTML=xmlhttp.responseText;
//to display the response in the html container
}
}
现在发送请求php文件和参数可以使用url查询字符串或post变量获取以及使用open()
方法
这里假设我们正在使用url查询字符串,
xmlhttp.open("GET","urphpfile.php?q="+str,true);
xmlhttp.send();
不要在php文件中接收“q”变量
$q=$_GET["q"];// here you may use $_REQUEST["q"] which can hadle both $_POST and $_GET
在你的php文件....
你完成了......