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来进行通信。

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