简体   繁体   English

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

[英]losing first data element jquery ajax call

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. 我使用的是tableDND jQuery插件,它允许我拖放表格的行。 I want to send those rows to the server and have them uploaded to a mySQL database. 我想将这些行发送到服务器,并将它们上载到mySQL数据库。

My code is below, basically when I get to the PHP the first element of the batting variable is gone. 我的代码如下,基本上,当我进入PHP时,打击变量的第一个元素消失了。 Any ideas? 有任何想法吗?

So I have the following javascript: 所以我有以下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:

<?php

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

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

?>

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. 这似乎很愚蠢,但是您可以通过用双引号括起来的“ batt”来避免整个问题。 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. 这会将您期望的$ _POST ['order']变量更改为“ $ _POST ['batting”]。但是,您的数据对象应像上面注释中最初提到的那样包含。

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. 在php中尝试var_dump($ _ POST)以查看您得到的所有值...只是出于好奇。

**Sorry for all the edits. **对不起,所有修改。 I'm being distracted constantly at work... 我在工作中经常分心...

您是否尝试过删除'+击球+'....两侧的空格,例如'+击球+'....

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

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