ajax-return value-when using AJAX, why use get can get the value in the background PHP page, and Post does not?

Source: Internet
Author: User
Keywords php ajax-return value Post get
    1. Get mode

    document.getElementById ("Save"). Onclick=function () {var request=new xmlhttprequest (); Request.open ("GET", "Http://localhost/service.php?username=" +document.getelementbyid ("Stuname"). Value+ "& Number= "+document.getelementbyid (" Stunumber "). value+" &sex= "+document.getelementbyid (" Stusex "). Value+" &            job= "+document.getelementbyid (" Stujob "). Value);                           Request.send (NULL); Request.onreadystatechange=function () {if (request.readystate===4) {if (request.st                        atus===200) {document.getElementById ("Saveresult"). Innerhtml=request.responsetext;                        }else{alert ("An error occurred in LA:" +request.status);   }}}}service.php $username =$_get[' username '];    $password =$_get["number"];    $sex =$_get["Sex"];      $job =$_get["Job"]; if ($username = = "One") && ($password) && ($sex) &Amp;& ($job)) {echo ' {"Success": "True", "MSG": "Student added Success"};   }else{Echo ' {"Success": "false", "MSG": "Parameter error, fill in the information is not full"}; The result: is correct.        However, for post delivery, there is a problem with the code as follows: document.getElementById ("Save"). Onclick=function () {var request=new xmlhttprequest (); var data= "name1=" +document.getelementbyid ("Stuname"). value+ "&number=" +document.getelementbyid ("Stunumber").   Value+ "&sex=" +document.getelementbyid ("Stusex"). value+ "&job=" +document.getelementbyid ("Stujob"). Value;        Request.open ("POST", "http://localhost/service.php");    Request.setrequestheader ("Content-type", "appliaction/x-www-form-urlencoded");        Request.send (data); Request.onreadystatechange=function () {if (request.readystate===4) {if (request.status===2                        XX) {document.getElementById ("Saveresult"). Innerhtml=request.responsetext;                        } else{alert ("An error occurred in LA:" +request.status);}}}} service.php code: Var_dump ($_server[' request_method ');//For testing $use   rname=$_post[' name1 '];    $password =$_post["number"];    $sex =$_post["Sex"];      $job =$_post["Job"]; if ($username = = "One") && ($password) && ($sex) && ($job)) {echo ' {"Success": "True", "MSG": "Student added to      "}";    } else{Echo ' {"Success": "false", "MSG": "Parameter error, fill in the information is not full"};     }

The result is:

String (4) "POST"
notice:undefined index:name1 in d:phpstudywwwservice.php on line 23
notice:undefined Index:number in d:phpstudywwwservice.php on line 24
notice:undefined Index:sex in d:phpstudywwwservice.php on line 25
notice:undefined index:job in d:phpstudywwwservice.php on line 26
{"Success": "false", "MSG": "Parameter error, fill in information not complete"}
, what is this for? Where did I write the question? Please expert guidance, thank you!

Reply content:

    1. Get mode

    document.getElementById ("Save"). Onclick=function () {var request=new xmlhttprequest (); Request.open ("GET", "Http://localhost/service.php?username=" +document.getelementbyid ("Stuname"). Value+ "& Number= "+document.getelementbyid (" Stunumber "). value+" &sex= "+document.getelementbyid (" Stusex "). Value+" &            job= "+document.getelementbyid (" Stujob "). Value);                           Request.send (NULL); Request.onreadystatechange=function () {if (request.readystate===4) {if (request.st                        atus===200) {document.getElementById ("Saveresult"). Innerhtml=request.responsetext;                        }else{alert ("An error occurred in LA:" +request.status);   }}}}service.php $username =$_get[' username '];    $password =$_get["number"];    $sex =$_get["Sex"];      $job =$_get["Job"]; if ($username = = "One") && ($password) && ($sex) &Amp;& ($job)) {echo ' {"Success": "True", "MSG": "Student added Success"};   }else{Echo ' {"Success": "false", "MSG": "Parameter error, fill in the information is not full"}; The result: is correct.        However, for post delivery, there is a problem with the code as follows: document.getElementById ("Save"). Onclick=function () {var request=new xmlhttprequest (); var data= "name1=" +document.getelementbyid ("Stuname"). value+ "&number=" +document.getelementbyid ("Stunumber").   Value+ "&sex=" +document.getelementbyid ("Stusex"). value+ "&job=" +document.getelementbyid ("Stujob"). Value;        Request.open ("POST", "http://localhost/service.php");    Request.setrequestheader ("Content-type", "appliaction/x-www-form-urlencoded");        Request.send (data); Request.onreadystatechange=function () {if (request.readystate===4) {if (request.status===2                        XX) {document.getElementById ("Saveresult"). Innerhtml=request.responsetext;                        } else{alert ("An error occurred in LA:" +request.status);}}}} service.php code: Var_dump ($_server[' request_method ');//For testing $use   rname=$_post[' name1 '];    $password =$_post["number"];    $sex =$_post["Sex"];      $job =$_post["Job"]; if ($username = = "One") && ($password) && ($sex) && ($job)) {echo ' {"Success": "True", "MSG": "Student added to      "}";    } else{Echo ' {"Success": "false", "MSG": "Parameter error, fill in the information is not full"};     }

The result is:

String (4) "POST"
notice:undefined index:name1 in d:phpstudywwwservice.php on line 23
notice:undefined Index:number in d:phpstudywwwservice.php on line 24
notice:undefined Index:sex in d:phpstudywwwservice.php on line 25
notice:undefined index:job in d:phpstudywwwservice.php on line 26
{"Success": "false", "MSG": "Parameter error, fill in information not complete"}
, what is this for? Where did I write the question? Please expert guidance, thank you!

Post with $_post fetch
If you want to take the get again, you can use $_request.

Because you're using $_get in PHP, you can only get the values that get passed in.

$_get getting parameters in a URL connection
$_post get the parameters of the POST submission
$_request can get parameters to get and post

In your case, using $_get is not getting the post parameters.

The parameter format is incorrect when the post request is changed to var postdata = {"name1": "Value1", "name2": "value2"} so try it. You can debug it F12.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.