3

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.

6
  • Try data: JSON.stringify(postData),
    – Tushar
    Commented Oct 21, 2015 at 4:54
  • @Tushar Not working. Internal Server error only, saying unable to decode value Commented Oct 21, 2015 at 5:00
  • 1
    Try contentType: 'application/x-www-form-urlencoded; charset=UTF-8', data: {json: JSON.stringify(postData),
    – Tushar
    Commented Oct 21, 2015 at 5:01
  • I think this worked, but how do I see if the string is being transferred to php or not. I tried doing echo, but nothing is being displayed. Commented Oct 21, 2015 at 5:13
  • why not just var_dump($_REQUEST) to see what you are getting ? also try what @Tushar is telling Commented Oct 21, 2015 at 5:22

5 Answers 5

3

The 500 (internal server error) means something went wrong on the server's side. So check the apache error log for more details

you may find apache log here /var/log/apache2/

2

On client side (javascript code):

data: JSON.stringify(postData)

On server side (PHP code):

json_decode($_POST["data"])
1

u commented the closing braces of the function with the return statement. change this :

//return JSON.stringify(postData);}

to:

//return JSON.stringify(postData);
}

Also :

data: JSON.stringify(postData),

IN update.php

$json = $_POST['myData'];
$result = json_decode($myData);

var_dump($result);
4
  • sorry, that was a typo, updated my question to reflect the change Commented Oct 21, 2015 at 5:05
  • i have no idea what that has to do with internal server error Commented Oct 21, 2015 at 5:08
  • ini_set("display_errors", "1"); error_reporting(E_ALL); u can try putting these to check for any errors. or check for apache error log
    – Nero
    Commented Oct 21, 2015 at 5:11
  • @NullPoiиteя I'm able to overcome the internal server error, however how do I make sure that the json object is being passed correctly ? can you pls help Commented Oct 21, 2015 at 5:18
0

Comment from @Tushar helped me resolving the issue

contentType: 'application/x-www-form-urlencoded; charset=UTF-8', data: {json: JSON.stringify(postData)
0

sent Ajax request to php Page to Get data

$.ajax({ method: "POST", // it may be Get url: "some.php", //page where you sent request data: { name: "John", location: "Boston" } //attibutes you want to take on that page }) .done(function( msg ) { // sucessfull reponse alert( "Data Saved: " + msg ); });

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.