Rais February 2016

Array object to php using ajax

How can i send data like this to php using ajax

["{"title":"mr","fname":"john","lname":"Annah","oname":"Clement","staffid":"123"}"]

Answers


Rogin Thomas February 2016

stringify before sending Eg :

var postData = [
{ "id":"1", "name":"bob"},
{ "id":"2", "name":"jonas"}]

this works,

$.ajax({
url: Url,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(postData) //stringify is important,
});


sudhakar February 2016

try json_encode for more refer - http://php.net/manual/en/function.json-encode.php


Ognjen Babic February 2016

Do it like so, using jQuery(which you need to include in your script):

 <script>
    var data={};

    data= {
    "title":"mr",
    "fname":"john",
    "lname":"Annah",
    "oname":"Clement",
    "staffid":"123"};

   $.ajax({
            url:"somwhere.php",
            type:"POST",
            dataType:"JSON",
            data:data,
            async: true});

    </script>

And on the page where you want to catch this data, do it like this:

<?php
$title=$_POST['title'];
$fname=$_POST['fname'];
?>

And so on.


paranoid February 2016

Try this

$(document).on("click", "#your element", function () {
   $.ajax({
    type: 'POST',
    url: "your_url",
    data : {"title":"mr","fname":"john","lname":"Annah","oname":"Clement","staffid":"123"},,
    success: function (result) {
        ### your action after ajax
    },
   })
})


Ranjith February 2016

you can pass it in data like this,

$.ajax({
        url: 'url',
        type: 'GET',
        data: {  title:"mr",fname:"john",lname:"Annah",oname:"Clement",staffid:"123" } ,
        contentType: 'application/json; charset=utf-8',
        success: function (response) {
            //your success code
        }
    });

Post Status

Asked in February 2016
Viewed 3,979 times
Voted 9
Answered 5 times

Search




Leave an answer