简体   繁体   中英

How to pass jQuery Serialize() data to another page using ajax

My form is -

<div id="formInfo">
    <form action="">
      <input type="text" name="uname" value="Chris">Name</input>
      <input type="text" name="address" value="NJ">Address</input>
      <input type="text" name="contact" value="123">Contact</input>
      <button id=""type="button" class="GrayButton" onclick="loadXMLDoc()">Proceed</button>
    </form>
</div>

I want to serialize this form's data and load another page using ajax which uses these data.

My ajax script look's like this -

 function loadXMLDoc()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("ShowAddNewProject").innerHTML=xmlhttp.responseText;
        }
      }
    var item= $("form").serialize();
    xmlhttp.open("GET","addprojectp1.php?q="+item,true);
    xmlhttp.send();
    }

In addprojectp1.php -

$qn = $_GET['q'];
echo $qn;

On button click i am correctly move to addprojectp1.php but value of $qn is spaces. I am expecting $qn = uname=Chris&address=NJ&contact=123 . Not sure what mistake i am doing. Please help.

Serialize won't give you the format you want. What you can do, however, is this:

$qn = unserialize($_GET['q']);

This way, $qn will be an array containing the data you sent through the GET request.

You can then do this:

var_dump($qn);

to see the format of the data in the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM