开发者

PHP Post not working with Ajax

I have a simple form:

<form id="formTest" name="formTest" action="" method="get">

<input id="txtPostcode" name="Postcode" type="text" class="txtBoxSmall" /> <input type="butt开发者_开发问答on" name="SubmitTheForm" id="btnSubmit" onClick="TestAjax()" value="submit" />

</form>

My Javascript code is:

function TestAjax(){
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {

        }
    };
    xmlhttp.open("GET","autocomplete.php?value1=aaaaa&value2=fffff",true);
    xmlhttp.send();
}

My problem is that in the php file autocomplete.php i can not access the txtPostcode element like so:

$postcodetext = $_GET[Postcode];

But if i get rid of the javascript function in the submit button, and add action="autocomplete.php" to the form tag it will work, but then of course it is not ajaxed. Can someone tell me why I cant get any values from $_GET[Postcode] when ajaxing?? I know i can just pass the value of the txtPostcode in the URL, but i dont want to do it that way, is there something i can do so i can access the textbox via the $_GET[Postcode] call in php??

Thanks.


You need to change this line:

xmlhttp.open("GET","autocomplete.php?value1=aaaaa&value2=fffff",true);

to include all the values you want to get in $_GET[] in PHP. You can do:

var postcode = document.getElementById('txtPostcode').value;
xmlhttp.open("GET","autocomplete.php?value1=aaaaa&value2=fffff&Postcode=" + postcode,true);

and similar for any additional things you want to access in PHP.

I totally agree with the comments below - take a look at jQuery, it will make your life much easier. Start here for example:

  • http://docs.jquery.com/How_jQuery_Works
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜