2012-09-13 145 views
0

所以我有幾個文本框,下拉菜單和這個表單上的電臺選項。當用戶點擊提交時,我想保存所有的信息,這樣我就可以把它放到數據庫中。因此,這是所有的表單的輸入Jquery序列化數據

<div id="reg2a"> 
First Name: <br/><input type="text" name="fname" /> <br/> 
Last Name: <br/><input type="text" name="lname" /> <br/> 
Address: <br/><input type="text" name="address" /> <br/> 
City: <br/><input type="text" name="city" /> <br/> 
State: <br/><input type="text" name="state" /> <br/> 
Zip Code: <br/><input type="text" name="zip" /> <br/> 
Phone Number: <br/><input type="text" name="phone" /> <br/> 
Fax: <br/><input type="text" name="fax" /> <br/> 
Email: <br/><input type="text" name="email" /> <br/> 
Ethnicity: <i>Used only for grant reporting purposes</i> <br/><input type="text" name="ethnicity" /> <br/><br/> 

Instutional Information Type (select the best option) <br/> 
<select name="iitype"> 
<option value="none">None</option> 
<option value="uni">University</option> 
<option value="commorg">Community Organization</option> 
</select> <br/><br/> 

    Number of sessions willing to present: 
    <select id="vennum_select" name="vnum"> 
     <?php for($i=0;$i<=3;$i++) { ?> 
     <option value="<?php echo $i ?>"><?php echo $i ?></option> 
     <?php } ?><br/> 
    </select><br/> 
    Number of tables requested: 
    <select id="tabnum_select" name="tnum"> 

     <?php for($i=1;$i<=3;$i++) { ?> 
     <option value="<?php echo $i ?>"><?php echo $i ?></option> 
     <?php } ?> 
    </select><br/><br/> 
    Awarding of a door prize during the conference will result in a reduction in the cost of your first table. <br/><br/> 
    I am providing a door prize for delivery during the conference of $75 or more 
    <select id="prize_select" name="pnum"> 
     <option value="0">No</option> 
     <option value="1">Yes</option> 
    </select><br/> 

Prize name: <input type="text" name="prize_name" /><br/> 
Description: <input type="text" name="descr" /><br/> 
Value: <input type="text" name="retail" /><br/><br/> 

Name of Institution: <br/><input type="text" name="institution" /> <br/><br/> 

Type (select the best option) <br/> 
<select name="type"> 
<option value="none">None</option> 
<option value="k5">K-5</option> 
<option value="k8">K-8</option> 
<option value="68">6-8</option> 
<option value="912">9-12</option> 
</select> <br/><br/> 

Address: <br/><input type="text" name="address_sch" /> <br/> 
City: <br/><input type="text" name="city_sch" /> <br/> 
State: <br/><input type="text" name="state_sch" /> <br/> 
Zip Code: <br/><input type="text" name="zip_sch" /> <br/> 

<form name="frm2sub" id="frm2sub" action="page3.php" method="post"> 
<input type="submit" name="submit" value="Submit" id="submit" /> 
</form> 
</div> 

這是我的jQuery函數:

$("#frm2sub").submit(function() { 
    var values = {}; 
values["fname"] = $("#fname").val(); 
}); 

我可以爲輸入框每一個做到這一點,但我想給所有這些數據到下一頁。那麼如何將這個數組放入$ _POST?順便說一句,我試着做

var data = $("#reg2a").serialize(); 

var data = $(document).serialize(); 

數據最終被空。有任何想法嗎?謝謝

回答

1

您應該將所有輸入元素包裝在表單標籤中。然後,所有數據將在$ _POST變量提供page3.php

像這樣:

<form name="frm2sub" id="frm2sub" action="page3.php" method="post"> 
    <div id="reg2a"> 
    First Name: <br/><input type="text" name="fname" /> <br/> 
    Last Name: <br/><input type="text" name="lname" /> <br/> 
    Address: <br/><input type="text" name="address" /> <br/> 
    City: <br/><input type="text" name="city" /> <br/> 
    State: <br/><input type="text" name="state" /> <br/> 
    Zip Code: <br/><input type="text" name="zip" /> <br/> 
    Phone Number: <br/><input type="text" name="phone" /> <br/> 
    Fax: <br/><input type="text" name="fax" /> <br/> 
    Email: <br/><input type="text" name="email" /> <br/> 
    Ethnicity: <i>Used only for grant reporting purposes</i> <br/><input type="text" name="ethnicity" /> <br/><br/> 

    Instutional Information Type (select the best option) <br/> 
    <select name="iitype"> 
    <option value="none">None</option> 
    <option value="uni">University</option> 
    <option value="commorg">Community Organization</option> 
    </select> <br/><br/> 

     Number of sessions willing to present: 
     <select id="vennum_select" name="vnum"> 
      <?php for($i=0;$i<=3;$i++) { ?> 
      <option value="<?php echo $i ?>"><?php echo $i ?></option> 
      <?php } ?><br/> 
     </select><br/> 
     Number of tables requested: 
     <select id="tabnum_select" name="tnum"> 

      <?php for($i=1;$i<=3;$i++) { ?> 
      <option value="<?php echo $i ?>"><?php echo $i ?></option> 
      <?php } ?> 
     </select><br/><br/> 
     Awarding of a door prize during the conference will result in a reduction in the cost of your first table. <br/><br/> 
     I am providing a door prize for delivery during the conference of $75 or more 
     <select id="prize_select" name="pnum"> 
      <option value="0">No</option> 
      <option value="1">Yes</option> 
     </select><br/> 

    Prize name: <input type="text" name="prize_name" /><br/> 
    Description: <input type="text" name="descr" /><br/> 
    Value: <input type="text" name="retail" /><br/><br/> 

    Name of Institution: <br/><input type="text" name="institution" /> <br/><br/> 

    Type (select the best option) <br/> 
    <select name="type"> 
    <option value="none">None</option> 
    <option value="k5">K-5</option> 
    <option value="k8">K-8</option> 
    <option value="68">6-8</option> 
    <option value="912">9-12</option> 
    </select> <br/><br/> 

    Address: <br/><input type="text" name="address_sch" /> <br/> 
    City: <br/><input type="text" name="city_sch" /> <br/> 
    State: <br/><input type="text" name="state_sch" /> <br/> 
    Zip Code: <br/><input type="text" name="zip_sch" /> <br/> 


    <input type="submit" name="submit" value="Submit" id="submit" /> 

    </div> 
</form> 
0

您應該序列化FORM標記的ID。但是,真的,您還沒有使用AJAX來發布表單,所以$ _POST實際上是通過發佈數據來實現的。使用

<form action="process.php" method="post"> 

然後,只需提交和$ _ POST將保存數據...

3

您需要將表格周圍的輸入,然後給予形式的ID和使用jQuery的serialize功能。這會給你一個URL編碼的所有值的字符串。

0

你的周圍形成沒有標記。添加表單標籤並序列化。這將採取表格內的所有元素

$('#myform').serialize();