jquery - 将一维数组发布到 db ajax post

标签 jquery html ajax forms serializearray

我有这个表单,它使用 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());

这段代码在一个表单中,底部有一个提交按钮。此 for 被发布到 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 序列化其数据的方式。

关于jquery - 将一维数组发布到 db ajax post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26423394/

相关文章:

javascript - 将 setTimeOut 绑定(bind)到具有特定元素的某个函数

javascript - 如何在点击时添加事件类并防止子菜单向上滑动

javascript - 我的 html 表格的分页没有显示

jquery - 使用返回的 JSON 填充数据库表

PHP + Mysql insert into + jQuery 进度条

jquery - 淡出内容 A 的 div,淡入内容 B 的同一 div

javascript - Bootstrap 4 - 下拉菜单 - 元素在导航栏上

javascript - 使用下拉菜单选择并显示一个图表

jQuery UI - 将 jQuery 可拖动对象恢复为其原始状态?

html - Tumblr 页面上无法解释的逗号