使用JQuery AJAX将值传递给PHP无法正常工作,数组混淆

时间:2016-01-27 18:48:26

标签: javascript php jquery html ajax

我在网上搜索了一个星期,发现有15个问题和我一样,但我没有找到正确的解决方案。
问题很简单。我在一个页面中有一个HTML表格,我想通过AJAX(Jquery)传递这个表并执行一个PHP脚本,但我不知道我在哪里做错了,这里所有的代码,如果需要我可以发布更多。

到目前为止ajax(这里真正的问题是数组定义?):

$(function(){
  $('#nice_button').on('click', function(e){
    // Using the core $.ajax() method
    $.ajax({
        // The URL for the request
        url: "ajax_insert_suba_def.php",

        var data2 = [{
            value1 :  [1,2,3,4,5],
            value2 :  [5,4,3,2,1]
        }];

        // The data to send (will be converted to a query string)
        data: { 'data': data2 },
        // Whether this is a POST or GET request
        type: "POST",
        // The type of data we expect back
        // dataType : "json",
        // Code to run regardless of success or failure
        complete: function( xhr, status ) {
            alert( "The request is complete!" );
        }
    });
  });
});

使用此HTML:

<form method="post" name="my_form">
    <button id="nice_button" type="button" name="btn_go" class="btn btn-success btn-lg"> Insert into DB</button>
</form>

使用一些基本的PHP :(功能还可以...... 我还需要知道如何正确地获取&#39;这些值)

<?php

    header("Content-Type: application/json");

    require "includes/functions.php" ;

    $my_sqli = connect_to_mysql();

    $data = $_POST['data'];


    $val_1 = $data["value1"][0];
    $val_2 = $data["value2"][0];

    $query = "INSERT INTO test_json (test_text) VALUES('" . $val_1 . "'); ";

    $result = $my_sqli->query($query);

    $my_sqli->close();

    return "ok";

?>

到目前为止,我得到的错误是:

SyntaxError: missing : after property id

(在var data2 = ... line中)

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

试试这个。在进行AJAX调用时,您犯了一些语法错误。请参阅 documentation 。您试图在ajax函数调用选项中定义变量,该变量应该是对象格式。

var data2 = {
    value1 :  [1,2,3,4,5],
    value2 :  [5,4,3,2,1]
};

$(function(){
  $('#nice_button').on('click', function(e){
    // Using the core $.ajax() method
    $.ajax({
        // The URL for the request
        url: "ajax_insert_suba_def.php",
        // The data to send (will be converted to a query string)
        data: data2,
        // Whether this is a POST or GET request
        type: "POST",
        // The type of data we expect back
        // dataType : "json",
        // Code to run regardless of success or failure
        complete: function( xhr, status ) {
            alert( "The request is complete!" );
        }
    });
  });
});

然后你将它作为两个阵列得到它,你可以像这样得到它们。

value1 = $_POST['value1'];
value2 = $_POST['value2'];

答案 1 :(得分:2)

  

SyntaxError:missing:属性id

之后

您必须在AJAX请求之外取data2,如下所示:

$(function(){
    $('#nice_button').on('click', function(e){

        var data2 = 
        [
            {
                value1 :  [1,2,3,4,5],
                value2 :  [5,4,3,2,1]
            }
        ]; 

        // Using the core $.ajax() method
        $.ajax({

            // The URL for the request
            url: "ajax_insert_suba_def.php",

            // The data to send (will be converted to a query string)
            data: { data: data2 },

            // Whether this is a POST or GET request
            type: "POST",

            // The type of data we expect back
            // dataType : "json",

            // Code to run regardless of success or failure
            complete: function( xhr, status ) {
                alert( "The request is complete!" );
            }
        });
    });
});
  

我还需要知道如何正确“获取”这些值

这是您获取个人价值的方法:

<?php

    // your code

    $data = $_POST['data'];

    $val_1 = $data[0]['value1'][0];
    $val_2 = $data[0]['value1'][1];

    // so on

    $val_5 = $data[0]['value1'][4];
    $val_6 = $data[0]['value2'][0];
    $val_7 = $data[0]['value2'][1];

    // so on

    $val_10 = $data[0]['value2'][4];

    // your code

?>