0

i am trying to post data using json.stringify
data like this

    <script>   
     $('.create-invoice').on('click', function()
            {
                grab_invoice_data();
                // Declare a variable
                var jsonObj = invoice_data;

                // Lets convert our JSON object
                var postData = JSON.stringify(jsonObj);

                // Lets put our stringified json into a variable for posting
                var postArray = {json: postData};

                $.download("json.php", postArray, 'post');`enter code here`
            })

    </script>    

// php has like this 

   <?php
$arrData = json_decode($_POST['json'],true);
extract($arrData , EXTR_PREFIX_SAME, "wddx");

$firstName = $arrData['name'];
$address1 = $arrData['address1'];
$address2 = $arrData['address2'];
$city = $arrData['city'];
$state = $arrData['state'];
echo $firstName;
echo $address1;
echo $address2;
?>

Notice: Undefined index: name in C:\xampp\htdocs\ASK_Soft\json.php on line 7

Notice: Undefined index: address1 in C:\xampp\htdocs\ASK_Soft\json.php on line 8

Notice: Undefined index: address2 in C:\xampp\htdocs\ASK_Soft\json.php on line 9

Notice: Undefined index: city in C:\xampp\htdocs\ASK_Soft\json.php on line 10 . . thanks.

0

3 Answers 3

1

Write below code in your php script

$arrData = json_decode($youdata,true); // this will give you data in array format

extract($arrData , EXTR_PREFIX_SAME, "wddx");

now you can..

echo $address1;
echo $address2; //and so on
Sign up to request clarification or add additional context in comments.

6 Comments

To refer extract function pls go to php.net/manual/en/function.extract.php URL
you this line $data = json_decode(file_get_contents('php://input'), true); "$data" variable
yout line json_decode(file_get_contents('php://input'), true); is giving you null data. it should given you a array
replace this file_get_contents('php://input') with $_POST['json']
replace this file_get_contents('php://input') with $_POST['json']
|
0

json_decodegives you an object so:

$firstName = $data->{'name'};
$address = $data->{'address'};

php.net - json_decode

Comments

0

after most try i got answer

   <?php
$arrData = json_decode($_POST['json'],true);
extract($arrData , EXTR_PREFIX_SAME, "wddx");

$firstName = $arrData['name'];
$address1 = $arrData['address1'];
$address2 = $arrData['address2'];
$city = $arrData['city'];
$state = $arrData['state'];
echo $firstName;
echo $address1;
echo $address2;
?>

Thanking Lalit Sharma for this answer

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.