0

I have a php file where user will fill the form and the form data will be send to the backend php file in JSON format but i am unable to retrieve the JSON data send to the php file

// DATA SENDING ...

$(document).ready(function () {
            $('#submit').click(function () {
                var name, id, sec, base;
                name = $('#name').val();
                id = $('#id').val();
                sec = $('#pay').val();
                base = [{name: name, reg: id, sec: sec}];
                $.ajax({
                    url: '/post/json.php',
                    type: 'POST',
                    data: JSON.stringify(base),
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    async: false,
                    success: function (json) {
                        //do something ... after the data successfully processed via json.php 
                        }
                    }
                });
            });
        });

i tried to write the php as simple file to get query string and echoing it but nothing happen ... but if i POST simple query string (via jQuery) then its echoing

// PHP BACKEND

<?php
  $ar= $_SERVER['QUERY_STRING'];    
  echo $ar;
?>
sudo
  • 906
  • 2
  • 14
  • 32

6 Answers6

0

Please read this page: Get JSON object from URL

$json = file_get_contents('url_here'); 
$obj = json_decode($json); echo
$obj->access_token;

also $_SERVER['QUERY_STRING']; won't work because you send data via POST.

use $_POST['base'] instead.

Community
  • 1
  • 1
Snake Eyes
  • 16,287
  • 34
  • 113
  • 221
0

You are using POST as request type, therefore all data submitted ('name', 'red', 'sec') is available in the pre-defined $_POST array.

Also, your JavaScript could be simplified:

var name = $('#name').val();
var id = $('#id').val();
var sec = $('#pay').val();
$.post( "/post/json.php", {name: name, reg: id, sec: sec}, function( data ) {
    ...
});
Calonthar
  • 168
  • 3
  • 8
0

You need to get POST data, this will not append as a query string, try to use $_POST or send it to a variable via post.

Try to use:

data: { base : JSON.stringify(base)},
// or data:'base =' + JSON.stringify(base),

Then get it on php file

$data = json_decode($_POST['base']);

For more: How to send json string to a php file using jquery submit?

Community
  • 1
  • 1
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
0

You are using the http POST request to submit the data on server which is accessible via
$_POST superglobal array.

$_SERVER['QUERY_STRING'] will be useful if you use http GET request.

simply use $_POST['name'], $_POST['reg'] etc. to access submitted data.

xlembouras
  • 8,215
  • 4
  • 33
  • 42
  • firstly i used so ... but $_POST[name]; etc.. also didn't work for me so i use $_SERVER['QUERY_STRING'] but nothing achieved yet – sudo Apr 30 '14 at 06:52
  • Please change this in your jquery code `data: "jsondata="+JSON.stringify(base);` and access it on server side using `$jsonArray = json_decode($_POST['jsondata']);` Then you can access required data using `$jsonArray['name']` – Pramod Patil Apr 30 '14 at 07:00
0

You can try it

$(document).ready(function () {
    $('#submit').click(function () {
    ....
    base = {name: name, reg: id, sec: sec};
        $.ajax({
            ...
            data: base,
            dataType: 'json',
            ...
        });
    });
});

And your php BACKEND

$post = json_decode($_POST);
lighter
  • 2,808
  • 3
  • 40
  • 59
0

You have not used post data key, the JSON data will be posted into single key,

Change

  data: JSON.stringify(base)

to

data: "json="+JSON.stringify(base)

And get data on server

print_r(json_decode($_POST['json']));
Girish
  • 11,907
  • 3
  • 34
  • 51
  • @SoumeshBanerjee did you check in ajax request firebug console..? – Girish Apr 30 '14 at 06:59
  • yes the params are successfully passed but no response is received from the server but if i just echo 'ok'; in backend then it respond ok when the file is invoked – sudo Apr 30 '14 at 07:04