dsovc00684 2011-10-25 02:29
浏览 20
已采纳

丢失第一个数据元素jquery ajax调用

I am having a problem that I cannot figure out.

I am using the tableDND jQuery add in, which allows me to drag and drop rows of a table. I want to send those rows to the server and have them uploaded to a mySQL database.

My code is below, basically when I get to the PHP the first element of the batting variable is gone. Any ideas?

So I have the following javascript:

$('#order').tableDnD({
        onDrop: function(table, row) {

            var batting = $.tableDnD.serialize();
            var id = <?php echo $id;?>;

            $.ajax({
                type: 'POST',
                url: 'update_batting.php',
                data: '{ "batting":' + batting + ', "id":'+ id +' }',
                dataType: 'json',
            });

        }
    });

and the following php:

<?php

$batting = json_encode($_POST['order']);

$query = "INSERT INTO test (t) VALUES ('$batting')";
mysql_query($query);

?>
  • 写回答

2条回答 默认 最新

  • dqgo99177 2011-10-25 03:00
    关注

    Ok so this is my crude guess at this one since I have to go soon...

    I think since "data" is converted to a query string it is being misread. I think your post is probably reading this:

    $_POST['batting'] = 'order[]=110209';
    $_POST['order'] = 'order[]=100245&order[]=100007&order[]=100296&order[]=10‌​0213&order[]=100071&order[]=100125&order[]=110206&order[]=110205&order[]=100152&o‌​rder[]=100247&order[]=100329&order[]=100299&order[]=100243';
    $_POST['id'] = '662852670';
    

    Because the first occurrence of the ampersand is terminating your variable.

    It might seem silly, but you can probably escape this whole problem by surrounding "batting" with double quotes. Effectively containing it as a string.

    data: '{ "batting":"' + batting + '", "id":'+ id +' }',
    

    This would change your expected $_POST['order'] variable to "$_POST['batting'] However, your data object should be contained like I originally mentioned in the comment above.

    data: {order: '"' + batting + '"', id:id},
    

    Instead of sending it as a string with no key as in your sample code.

    Try var_dump($_POST) in php to see all values you're getting... just out of curiosity.

    **Sorry for all the edits. I'm being distracted constantly at work...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算