javascript - AJAX Post form Variables to PHP Page - Stack Overflow

I have a form that executes an AJAX function to submit form values to a PHP page. The PHP page just res

I have a form that executes an AJAX function to submit form values to a PHP page. The PHP page just responds by echoing out the variables in a DIV.

It works with GET method, but I can't get it working with POST.

    <html>
    <body>
    <script language="javascript" type="text/javascript">
    <!-- 
    //Browser Support Code
    function ajaxFunction(){
        var ajaxRequest;

        try{
            // Opera 8.0+, Firefox, Safari
            ajaxRequest = new XMLHttpRequest();
        } catch (e){
            // Internet Explorer Browsers
            try{
                ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try{
                    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e){
                    // Something went wrong
                    alert("Browser Not Supported");
                    return false;
                }
            }
        }
        // Get Response
        ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById("response").innerHTML = ajaxRequest.responseText;
            }
        }
        var name=document.getElementById("name").value
        var email=document.getElementById("email").value
        ajaxRequest.open("POST", "process.php?name="+name+"&email="+email, true);
        ajaxRequest.send(null); 
    }
    //-->
    </script>
    <form name='Form1'>
      Name:  <input type='text'  name='name' id="name"/> <br />
      Email: <input type='text'  name='email' id="email"/> <br />
    <input type="button" value="Submit" onClick="ajaxFunction();">
    </form>
    <div id="response">
    </div>
    </body>
    </html>

And the php page, process.php

    <?php
    echo date("H:i:s"); 
    echo "<br/>";
    // echo "Post Variables: ".$_POST['name']." ".$POST['email']; old code
            echo "Post Variables: ".$_POST['name']." ".$_POST['email'];
    echo "<br/>";
    echo "Get Variables: ".$_GET['name']." ".$_GET['email'];
    ?>

The response I get is:

11:32:05 Post Variables: Get Variables: name entered email entered

So i'm pretty sure it's to do with the variable being passed from PHP to Javascript.

Many thanks.

I have a form that executes an AJAX function to submit form values to a PHP page. The PHP page just responds by echoing out the variables in a DIV.

It works with GET method, but I can't get it working with POST.

    <html>
    <body>
    <script language="javascript" type="text/javascript">
    <!-- 
    //Browser Support Code
    function ajaxFunction(){
        var ajaxRequest;

        try{
            // Opera 8.0+, Firefox, Safari
            ajaxRequest = new XMLHttpRequest();
        } catch (e){
            // Internet Explorer Browsers
            try{
                ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try{
                    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e){
                    // Something went wrong
                    alert("Browser Not Supported");
                    return false;
                }
            }
        }
        // Get Response
        ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById("response").innerHTML = ajaxRequest.responseText;
            }
        }
        var name=document.getElementById("name").value
        var email=document.getElementById("email").value
        ajaxRequest.open("POST", "process.php?name="+name+"&email="+email, true);
        ajaxRequest.send(null); 
    }
    //-->
    </script>
    <form name='Form1'>
      Name:  <input type='text'  name='name' id="name"/> <br />
      Email: <input type='text'  name='email' id="email"/> <br />
    <input type="button" value="Submit" onClick="ajaxFunction();">
    </form>
    <div id="response">
    </div>
    </body>
    </html>

And the php page, process.php

    <?php
    echo date("H:i:s"); 
    echo "<br/>";
    // echo "Post Variables: ".$_POST['name']." ".$POST['email']; old code
            echo "Post Variables: ".$_POST['name']." ".$_POST['email'];
    echo "<br/>";
    echo "Get Variables: ".$_GET['name']." ".$_GET['email'];
    ?>

The response I get is:

11:32:05 Post Variables: Get Variables: name entered email entered

So i'm pretty sure it's to do with the variable being passed from PHP to Javascript.

Many thanks.

Share Improve this question edited Jan 9, 2014 at 11:48 Paul asked Jan 9, 2014 at 11:33 PaulPaul 4312 gold badges10 silver badges24 bronze badges 8
  • 2 @MazIqbal The form method is irrelevant if you're using AJAX. – Barmar Commented Jan 9, 2014 at 11:35
  • By default the method with which the information is passed to the php script is Get. You must specify in your ajax to pass the information via POST. – gprusiiski Commented Jan 9, 2014 at 11:36
  • Yeah added method"post" no difference. The get is only working as I've appended the values to the URL, so the PHP page can request GET or POST ??? – Paul Commented Jan 9, 2014 at 11:38
  • 1 For one thing, this $POST['email']; should read as $_POST['email']; you forgot the underscore. @Paul – Funk Forty Niner Commented Jan 9, 2014 at 11:39
  • A little thing: in your process.php on line 4 you've written $POST['email'] - the underscore is missing. But that should not be the point... can you try to do a print_r($_REQUEST);? – Robert Commented Jan 9, 2014 at 11:39
 |  Show 3 more ments

1 Answer 1

Reset to default 7
ajaxRequest.open("POST", "process.php?name="+name+"&email="+email, true);
ajaxRequest.send(null);

That's not posting the variables. Here you're sending them as GET parameters, with an empty body for your POST request. Instead, use

ajaxRequest.open("POST", "process.php", true);
ajaxRequest.send("name="+name+"&email="+email);

or even better

ajaxRequest.open("POST", "process.php", true);
ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxRequest.send("name="+encodeURIComponent(name)+"&email="+encodeURIComponent(email));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745612451a4636029.html

相关推荐

  • javascript - AJAX Post form Variables to PHP Page - Stack Overflow

    I have a form that executes an AJAX function to submit form values to a PHP page. The PHP page just res

    12小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信