2013-01-08 23 views
-1

嗨,我真的很新開發web開發。我想知道如何在php中訪問ajax返回的值。 當我從我的下拉列表中選擇一個選項時,我使用ajax獲得另一個下拉列表。但我想使用php的頁面上的值。如何在php中訪問ajax返回的值

 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(); 
} 

阿賈克斯將返回在div「聯想」所需的下拉框和價值觀,但我怎麼能在PHP中使用這些值,而在提交表格?我只需要訪問並在屏幕上的某處回顯。請幫忙。 在此先感謝。

+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)。