使用 JQuery AJAX 将值传递给 PHP 不起作用,数组混乱


Passing values to PHP with JQuery AJAX not working, array confusion

我在网上搜索了大约 1 周,发现有 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:(函数还可以... 我还需要知道如何正确"获取"这些值

<?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

(在变量数据2= ...行)

感谢您的帮助!

试试这个。您在进行 AJAX 调用时犯了一些语法错误。请参阅文档。您尝试在 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'];

语法错误:缺少:在属性 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
?>