繁体   English   中英

将单个维度数组发布到db ajax post

[英]posting a single dimenstion array to db ajax post

我有这个表单,使用jquery加载函数加载细节这里是表单

 <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);
                }
            });

这段代码不起作用可以有人帮忙吗? 提前致谢。

JDK

我不知道这会有多大帮助但是尝试添加传统的ajax选项

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

这改变了Ajax序列化数据的方式。

实际上,你正在检查你的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;
    }
//}

我认为发送表单的问题是,您需要在表单中输入值,以便发送它们。

尝试在您尝试发送的所有输入中创建隐藏字段,并使用应该使用的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);
            }
        });

暂无
暂无

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

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