Kemal Duran
Kemal Duran

Reputation: 1522

ajax post array of integers and string

I have to send array of integers and a string value with ajax to php. How to do this and how to get these values on php side?

// values to be send
var nums = [1,2,3,4];
var method = "delete";

$.ajax({
    url: "ajaxcalls.php",
    type: "GET",
    dataType: "json",
    data: ???,
    contentType: 'application/json; charset=utf-8',     
});

// ajaxcalls.php
<?php
   ???
?>

Upvotes: 0

Views: 1812

Answers (1)

Musa
Musa

Reputation: 97707

Just pass the data in an object to $.ajax and jQuery will serialize the data for you.
In PHP you can simply retrieve the data via the $_GET super global.

// values to be send
var nums = [1,2,3,4];
var method = "delete";

$.ajax({
    url: "ajaxcalls.php",
    type: "GET",
    data: {"nums": nums, "method": method}

});
<?php
   $numArray = $_GET['nums'];
   $method = $_GET['method'];

Upvotes: 2

Related Questions