0

i Pass array to ajax request can any one suggest me how can i fetch array in my my php file . JavaScript

var value1 = [];  
$.each($('.form-control_1'), function() {
    var total_stock;
    var total_stock = this.id;
    //console.log(total_stock);
    value1.push(document.getElementById(total_stock).value)
    console.log(value1);
});

console.log('starting ajax');

$.ajax({
    url: "insert_inventory.php",
    type: "post",
    data: { value1: value1 },
    success: function (data) {
        var dataParsed = JSON.parse(data);
        console.log(dataParsed);
    }
});
6
  • can you give me any code or tutorial reference. Commented Nov 24, 2017 at 11:17
  • how can i make value1 an array and assign value @jeroen Commented Nov 24, 2017 at 11:27
  • 1
    var value1 = []; AND value1.push(document.getElementById(total_stock).value) Commented Nov 24, 2017 at 11:52
  • Thanks @Roy can you please tell me how can i fetch this array in insert_inventory.php Commented Nov 24, 2017 at 12:13
  • 1
    $data = $_POST['value1']; foreach($data as $value){ echo $value} Commented Nov 24, 2017 at 12:25

1 Answer 1

1

You can set an array like this:

var value1 = []; //Outside loop.
value1.push(document.getElementById(total_stock).value) //Inside loop.

And you have to fetch data in PHP like this way:

$data = $_POST['value1']; 
foreach($data as $value){ 
   echo $value;
}

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.