I have searched my problem before posting this question, but failed to find a solution. I need to send a json string to php file but unable to do so, can some one please help with my problems below: I'm new to php and jquery and is struggling, Need your cooperation please.
I have a function that captures data on the text file:
function updateVal() {
var node_list = document.getElementsByTagName('input');
var c = 0;
var fieldName = [];
var fieldText = []
var ID = [];
for (var i = 0; i < node_list.length; i++) {
var node = node_list[i];
if (node.getAttribute('type') == 'text') {
fieldName[c] = node.name;
fieldText[c] = node.value;
ID[c] = node.id;
c++;
}
}
var postData = {
fieldName: fieldName,
fieldText: fieldText,
ID: ID
};
var dataString = JSON.stringify(postData);
console.log(JSON.stringify(postData));
$.ajax({
type: "POST",
dataType: "json",
url: "update.php",
data: {myData:postData}
})
//return JSON.stringify(postData);
}
My update.php is like this:
<?php
$json = $_POST['json'];
$result = json_decode($json);
echo $result;
echo $_POST['myData']);?>
On loading this: I'm getting the following error:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Moreover, I'm not sure if the data being sent to php or not. Can experts pls validate.
data: JSON.stringify(postData),
contentType: 'application/x-www-form-urlencoded; charset=UTF-8', data: {json: JSON.stringify(postData),
var_dump($_REQUEST)
to see what you are getting ? also try what @Tushar is telling