简体   繁体   English

如何使用Ajax将jQuery Serialize()数据传递到另一个页面

[英]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. 我想序列化此表单的数据并使用使用这些数据的ajax加载另一个页面。

My ajax script look's like this - 我的ajax脚本看起来像这样-

 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 - 在addprojectp1.php中-

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

On button click i am correctly move to addprojectp1.php but value of $qn is spaces. 在按钮上单击,我正确地移动到addprojectp1.php但$ qn的值是空格。 I am expecting $qn = uname=Chris&address=NJ&contact=123 . 我期望$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. 这样, $ qn将是一个包含您通过GET请求发送的数据的数组。

You can then do this: 然后,您可以执行以下操作:

var_dump($qn);

to see the format of the data in the array. 查看数组中数据的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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