php與前端傳遞參數

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來進行通信。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章