2014-10-17 64 views
2

我有這樣的形式,即使用jQuery負載功能 這裏的形式發佈一個dimenstion陣列DB阿賈克斯後

<form id="form1" name="form1" method="post" action=""> 
<div id="tableBg"> 
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="list"> 
<thead> 
<tr> 
     <th width="39%" scope="col">WHITE LIST</th> 
     <th width="61%" scope="col">&nbsp;</th> 
     </tr> 
     </thead> 
</table> 

<div style="max-height:400px; overflow-y:auto"> 
    <table width="100%" border="0" cellspacing="0" cellpadding="0" id="list"> 
    <tr class="sub_head"> 
     <td scope="col"><strong>REG. NO.</strong></td> 
     <td scope="col"><strong>STUDENT NAME</strong></td> 
     <td scope="col"><strong>GRADE</strong></td> 
     <td scope="col"><strong>MESSAGE ID</strong></td> 
     </tr> 
    <tbody></tbody> 
    </table> 
</div> 
<div id="PageFooter"> 
<input type="submit" name="save_result" id="save_result" value="Save Results" /> 
</div> 
</div> 
</form> 

加載信息,並加載從另一個頁面

負載下面的代碼
do{ 
    ?> 
<tr <?php echo (($x%2) != 0) ? '' : ' class="alternate-row row"' ?>> 
    <td scope="col"><?php echo $learners['reg_no'];?></td> 
    <td scope="col"><?php echo $learners['surname']." ".$learners['full_names']." (".$learners['nickname'].")";?></td> 
    <td scope="col"><?php echo $learners['grd']." ".$learners['grade_section'];?></td> 
    <td scope="col"> 
    <input name="sms_id[<?php echo $learners['reg_no']; ?>]" type="text" size="3" /> 
    </td> 
    </tr> 
<?php $x++;}while($learners = $getLearners->fetch()); 

此代碼位於表單內,底部有一個提交按鈕。這對於將被髮送至該process.php用下面的代碼,打印的一些輸出

if(isset($_POST['submit'])){ 

    foreach($_POST['sms_id'] as $reg=>$msg_id){ 
     echo $reg. "/".$msg_id; 
    } 

}

上述顯示器的兩個值如果窗體從表單直接發佈到process.php

但我想使用ajax,並嘗試下面的代碼不工作。只需發佈$ msg_id而不是其他值。

var formData = $("#form1").serializeArray(); 
      $.ajax({ 
       url: "models/process.php", 
       type: "POST", 
       data: formData, 
       success: function(data) 
       { 
        alert(data); 
       } 
      }); 

此代碼無法幫助?提前致謝。

Ĵdķ

+0

你的'#form1'在html中是哪裏? – 2014-10-17 10:53:19

+0

@jogesh_pi我有更新的代碼,有一切。 – 2014-10-17 11:32:27

+0

**只需發佈$ msg_id而不是其他值。**這是什麼意思**其他值** – 2014-10-20 04:06:35

回答

3

其實,你正在檢查你的process.php if(isset($_POST['submit']))這是不及格的AJAX。

$('#form1').serializeArray()不包括用於提交按鈕的數據。

檢查這裏的文檔:http://api.jquery.com/serializearray/

你的PHP代碼(process.php)應改爲:

//if(isset($_POST['submit'])){ // this will not be available when using .serializeArray() 

    foreach($_POST['sms_id'] as $reg=>$msg_id){ 
     echo $reg. "/".$msg_id; 
    } 
//} 
1

我認爲這個問題是其發送的形式,你需要輸入值你的表格,所以你可以發送它們。

嘗試在隱藏字段中創建所有您嘗試發送的輸入,並使用form.serialize(),這應該足夠了。

例子:

<tr <?php echo (($x%2) != 0) ? '' : ' class="alternate-row row"' ?>> 
<td scope="col"><input type="hidden" value="Somevalue" name"attributeName"/> 
<?php echo  $learners['reg_no'];?></td> 
</tr> 
<?php $x++;}while($learners = $getLearners->fetch()); 

而且你的Ajax調用:

var formData = $("#form1").serialize(); 
     $.ajax({ 
      url: "models/process.php", 
      type: "POST", 
      data: formData, 
      success: function(data) 
      { 
       alert(data); 
      } 
     }); 
4

我不知道這是否會是多大的幫助,但嘗試添加傳統的AJAX選項

$.ajax({ 
     url: "models/process.php", 
     type: "POST", 
     traditional: true, 
     data: formData, 
     success: function(data) 
     { 
      alert(data); 
     } 
    }); 

這改變了Ajax序列化數據的方式。