简体   繁体   中英

How to send array in Ajax and get it in PHP as array

I want to send in ajax array in js, get it in php as an array and insert into with sql.

My ajax call looks like:

jQuery("#Save").click(function () {
  $(".linesView").each(function () {
    var action_name = "";
    if (window.current_category === "Expenses") {
      action_name = "Save_Expenses"
    } else if(window.current_category === "Incomes") {
      action_name = "Save_Incomes"
    }
    var line_id = $(this).attr("id").substring(5);
    var category = $("#CategoryValue_" + line_id).html();
    var date = $("#DateValue_" + line_id).html();
    var amount = $("#AmountValue_" + line_id).val();
    var repeated = $("#RepeatedValue_" + line_id).html();
    var note = $("#NoteValue_" + line_id).val();
    var data = json_encode([category,date,amount,repeated,note]);

    $.post("AjaxHandler.php", { "action_name": action_name, "data": data }, function () {
       //$("#ExpensesId_" + id).css('display', 'none');
    });
  });
});

The PHP code that needs to get the ajax call and add the data (by sql insert) looks like:

if(isset($_POST['action_name','data'])) {
  $action_name = $_POST['action_name'];
  $data=json_decode($_POST['data']);
  $query = mysql_query("INSERT INTO Expenses (accountid, category, date, amount, repeated) VALUES ('$accountid', '$data[0]', '$data[1]', '$data[2]', '0')");
}

The accountid coming from the top of the page, I already do delete action and it works fine, so the accountid is ok. All others, I don't know.

I tried to do encode and then decode. I am not sure if the syntax is right. Anyway if I didn't write elegant code, please show me how it needs to look. Maybe i need to take each param from the data and not to call data[x] ?

Use JSON.stringify()

var data = ([category,date,amount,repeated,note]);
$.post("AjaxHandler.php", { "action_name": action_name, "data": JSON.stringify(data) }, function () {
  //$("#ExpensesId_" + id).css('display', 'none');
});

Encode the data array to JSON string using JSON.stringify() in javascript. At server side use json_decode() to decode the data.

jQuery:

jQuery("#Save").click(function() {
  $(".linesView").each(function() {
    var action_name = "";
    if (window.current_category === "Expenses") {
      action_name = "Save_Expenses"
    } else if (window.current_category === "Incomes") {
      action_name = "Save_Incomes"
    }
    var line_id = $(this).attr("id").substring(5);
    var category = $("#CategoryValue_" + line_id).html();
    var date = $("#DateValue_" + line_id).html();
    var amount = $("#AmountValue_" + line_id).val();
    var repeated = $("#RepeatedValue_" + line_id).html();
    var note = $("#NoteValue_" + line_id).val();
    var data = JSON.stringify([category, date, amount, repeated, note]);
    //-----------------^--- Array to JSON string 
    $.post("AjaxHandler.php", {
      "action_name": action_name,
      "data": data
    }, function() {
      //$("#ExpensesId_" + id).css('display', 'none');
    });
  });
});

PHP :

if(isset($_POST['action_name','data'])){
    $action_name = $_POST['action_name'];
    $data=json_decode(json_decode($_POST['data']));
    //-----^--- decoding JSON string
    $query = mysql_query("INSERT INTO Expenses (accountid, category, date, amount, repeated) VALUES ('$accountid', '$data[0]', '$data[1]', '$data[2]', '0')");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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