php與前端傳遞參數
- 原生態javascript
function showSite(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
// IE7+, Firefox, Chrome, Opera, Safari 瀏覽器執行代碼
xmlhttp=new XMLHttpRequest();
}
else
{
// IE6, IE5 瀏覽器執行代碼
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
alert(xmlhttp.responseText);
}
}
//xmlhttp.open("GET","getsite_mysql.php?q="+str,true);
alert("hello");
xmlhttp.open("POST","getsite_mysql.php",true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//必需
xmlhttp.send("q="+str);
}
- 使用jquery
function showSite2(str)
{
$.ajax({ //ajax顯示發帖
url:'getsite_mysql.php',
type:'POST',
data: {"q":str},
async:true,
success:function(response,status,xhr){
//alert(response);
//document.getElementById("txtHint").innerHTML=response;
$('#txtHint').text("");
$('#txtHint').append(response);
}
});
}
也可以用包裝好的$.POST
和$.GET
來進行通信。