I am passing json object to my php file through ajax call.
jQuery Syntax
$.getJSON('http:..Sample/JsonCreation.php', function(data)
{
//data has the json - I am trying to edit this json and then pass it to another php file through ajax and save in DB. I have successfully edited but unable to access the passed json in my php file.
questionsArray = data;
}
$('form').on('submit', function(event)
{
//AJAX CALL
$.ajax
({
url: "updateTestReport.php",
type: "post",
//JSON.stringify
data: {json:(questionsArray)},
contenttype: "application/json; charset=utf-8",
datatype: "text",
success: function(data)
{
//alert("success");
alert(data);
},
error:function()
{
alert("failure");
}
});
});
PHP FILE
<?php
//$contentjson = file_get_contents("php://input");
//echo $_POST['json'];
$questionsArray = json_encode($_POST['json']);
echo $questionsArray;
?>
OUTPUT:
[{"questionNumber":"1","quesDesc":"The price of petrol has increased by 25%.By what percentage should it now be reduced to return to the original price?","optionA":"25%","optionB":"20%","optionC":"15%","optionD":null,"correctOption":"A","selectedAnswer":null,"isQuestionViewed":null},{"questionNumber":"2","quesDesc":"Price of cooking gas has increased by 10%. By what percentage should consumption be reduced to keep expenditure unchanged?","optionA":"9.09%","optionB":"10%","optionC":"11.11%","optionD":null,"correctOption":"A","selectedAnswer":null,"isQuestionViewed":null},{"questionNumber":"3","quesDesc":"A buys a shirt for Rs 100 and sells it to B for Rs 120. B now sells it to C for Rs 144. Had A sold it directly to C at the price C paid for it, then his profit % would have been","optionA":"10%","optionB":"20%","optionC":"44%","optionD":null,"correctOption":"C","selectedAnswer":null,"isQuestionViewed":null}]
1) How is that json_encode in the php file gives the output. Shouldn't i use json_decode? 2) And even through the encoded json, i am not able to retrieve the values. Please let me know how to access the values from this json... 3) what is the difference between file_get_contents & $_POST['json']?
EDIT: //BEGIN
I need to access my json inside the php file, retrieve some values, make some calculations, save in DB. Then return to the main html file.
//END Kindly give clear explanation...