如何在php中访问ajax返回值

时间:2013-01-08 17:34:13

标签: php ajax

嗨,我对网络开发很陌生。我想知道如何在php中访问ajax返回值。 当我从下拉列表中选择一个选项时,我会使用ajax获得另一个下拉列表。但我想使用php在页面上使用该值。

      Practice Name : 
    <select name="practiceName" id="practiceName" onchange="showAssociate(this.value)">
    <option value="">Select an option</option>
 <option value="abc">abc</option>
 <option value="xyz">xyz</option>


    </select>
<p>

<div id="associate">
</div>

ajax代码是:

    function showAssociate(str)
{
if (str=="")
{
document.getElementById("associate").innerHTML="";
return;
} 
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
 }
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("associate").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getAssociate.php?q="+str,true);
xmlhttp.send();
}

ajax返回所需的drop box和div'associate'中的值,但是如何在提交表单时在php中使用这些值?我只需要在屏幕上的某个地方访问并回显它。请帮忙。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果您希望通过ajax检索的数据与表单一起发送,则可以填充隐藏的输入,以便在表单提交中传输数据。

<input type="hidden" id="data" name="data" value="" />

在Javascript中,填充其值:

document.getElementById("associate").innerHTML=xmlhttp.responseText;
document.getElementById("data").value = xmlhttp.responseText;

在PHP方面,当您处理表单提交时,它将出现在$_POST['data']中(假设表单的方法是POST)。