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;
?>
3
  • You have the two built in functions json_decode and json_encode to work with json in php. Is it not enough? Commented Apr 30, 2014 at 6:37
  • 2
    POST data is sent as HTTP body not as query string..use $_POST in PHP Commented Apr 30, 2014 at 6:37
  • @AbhikChakraborty i used it already but nothing happened Commented Apr 30, 2014 at 7:05

6 Answers 6

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 ) {
    ...
});
Sign up to request clarification or add additional context in comments.

1 Comment

$data=$_POST['name']; this is also unable to echo the data echo $data
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.

2 Comments

i firstly used $_POST['name']; etc to echo the data but it didnt work
Use $_POST['base'], not name
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);

Comments

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']));

2 Comments

@SoumeshBanerjee did you check in ajax request firebug console..?
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
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.

2 Comments

firstly i used so ... but $_POST[name]; etc.. also didn't work for me so i use $_SERVER['QUERY_STRING'] but nothing achieved yet
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']
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?

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.