【问题标题】:save dynamic records in DB using Php, Ajax, JSON使用 Php、Ajax、JSON 在数据库中保存动态记录
【发布时间】:2014-04-17 20:41:16
【问题描述】:

我有一个表,每行包含 1 个数据库记录的字段
动态添加和删除表行
我想使用 ajax 将所有文本框数据发送到 php 文件
表格如下

<table id="tblDmc" frame="box" rules="all" width="100%">
            <tr>
                <th>Subject</th><th>Total Marks</th><th>Obtained</th><th>Class Highest</th><th>Remarks</th><th>&nbsp;&nbsp;&nbsp;</th>
            </tr>
            <tr class="subRecord">
                <td><input type="text" class="dmcTxt subject"></td>
                <td><input type="number" class="dmcTxt total"></td>
                <td><input type="number" class="dmcTxt obtained"></td>
                <td><input type="number" class="dmcTxt highest"></td>
                <td><input type="text" class="dmcTxt remarks"></td>
                <td><a href="#" class="removeRow">x<a></td>
            </tr>  
        </table>

Jquery Ajax 代码

$("#btnSave").click(function(){
    var data1 = [];     
    var stdRow = {"s_id":$("#drpClass").val(),"month":$("#drpMonth").val(),"percent":$("#percentage").val(),"grade":$("#grade").val(),"position":$("#position").val(),"curricular":$("#curricular").val()};
    data1.push(stdRow);
    $("#tblDmc .subRecord").each(function(){
        if($(this).find(".subject").val()!="")
        {
            var rowData = {};
            rowData["subject"] =$(this).find(".subject").val();
            rowData["total"] =$(this).find(".total").val();
            rowData["obtained"] =$(this).find(".obtained").val();
            rowData["highest"] =$(this).find(".highest").val();
            rowData["remarks"] =$(this).find(".remarks").val();
            data1.push(rowData);
        }
    });
    //$(this).after(JSON.stringify(data1));
    $.ajax({
        url:'savedmc.php',
        type:'POST',
        data: JSON.stringify(data1),
        contentType: "application/json; charset=UTF-8",
        success: function(e){
            alert(e);
        }
    });

});

JSON.stringify(data1) 返回以下内容

[
 {"std_id":"6","month":"4","percent":"77","grade":"1","position":"33","curricular":"abc"},     
 {"subject":"Maths","total":"100","obtained":"99","highest":"100","remarks":"remarks"},   
 {"subject":"Physics","total":"100","obtained":"94","highest":"99","remarks":"emarks"}   
]

savedmc.php 中的代码是

<?php print_r($_POST); ?>

返回 NULL array()

我的问题是如何通过 ajax 将上述 json 对象/数组发送到 php 文件,
或任何替代方案?

【问题讨论】:

  • 网络标签上显示的是什么(在 Chrome 上按 f12)??
  • method:post, status:200/ok, type:text/html, and time,size,initiator
  • 点击网络标签中的页面链接时不显示json数据?
  • 是的,它在表单数据选项卡中显示数据,我需要不同的东西来访问该数据吗?
  • 您能否在网络选项卡上发布数据的屏幕截图?

标签: php mysql ajax json


【解决方案1】:

用这个版本替换你的 JS 代码中的 ajax 调用:

$.ajax({
        url:'savedmc.php',
        type:'POST',
        dataType: "json",
        data: JSON.stringify(data1),
        contentType: "application/json; charset=UTF-8",
        success: function(e){
            alert(e);
        }
    });

【讨论】:

  • 还是没有收到,返回空数组
  • 您可以使用 firebug 或 chrome 控制台检查您使用 ajax 发送的参数吗?
  • 未定义=&未定义=
  • json 有效,还需要测试什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
  • 2014-08-11
  • 2020-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-30
相关资源
最近更新 更多