1

The code below triggers the error callback function. Can you help me ? Actually the thing I'm trying do is get the data in the form in php and then send back these data in Ajax for further treatments

HTML

<body>
    <div>
    <form id="user">
        NOM : <input type="text" name="name"/>
    </form>
        <button id="tst" onclick="SHOW();">CLICK</button>
    </div>

Javascript

function AFFICHER(){

$.ajax({
    type: 'POST',
    url: 'http://localhost:8012/myscript.php',
    data: $('#user').serialize(),
    dataType: "json",
    crossDomain: true,
    success: function(result) {
      console.log(result);
    },
    error: function(xhr, textStatus, errorThrown) {
     console.log('ajax loading error...');
     return false;
    }
});


}

My PHP

<?php
$decoded = json_decode($_POST['data'],true);
foreach ($decoded as $value) {
   echo json_encode($value["name"]);
}

?>

The error is

enter image description here

7
  • "The code below triggers the error callback function." - And the error is... ? Commented Jun 15, 2016 at 13:06
  • A. there is no need to json_decode() the $_POST data, B. $_POST['data'] isn't what you think it is, C. gather your array data inside the foreach() loop and json_encode() is post loop. Commented Jun 15, 2016 at 13:21
  • @David error is "ajax loading error..." as in my callback Commented Jun 15, 2016 at 13:24
  • 1
    @JasonKrs - You're passing 3 arguments to the error() callback function which can offer you greater detail as to what is happening: xhr, textStatus, errorThrown Use those instead of a generic, non-informative error message such as "ajax loading error" Commented Jun 15, 2016 at 13:25
  • 1
    In your PHP, all you need to do for such a simple example is: echo json_encode($_POST); and be done with it. And since you're return data is expected to be JSON (which it will be), you cannot console.log(result) and expect results. You must access the keys directly, ie. console.log(result.name), etc., as determined by the response data. Commented Jun 15, 2016 at 13:33

3 Answers 3

2

$('#user').serialize() will leave you with a string such as name=<input-value> (name being the input name from your form input name="name"). The larger your form gets, the more values that will be stored in the data string, Eg. name=marcus&phone=555-555-5555&location=interwebs

To directly access those in your PHP script, you fetch them from the $_POST array (since you specified type: 'POST' in your $.ajax() settings):

echo $_POST['name']; // marcus

You need not run it through json_decode() since your data is not coming in as JSON, nor can/do you access $_POST['data'] in the manner you are since that key does not exist in the $_POST array.

Your foreach() loop is redundant. The data in question (that you're looking to return) is already stored in the $_POST array.

Since your program is very basic, just encode the $_POST array and send it back. This is all you need in your PHP script:

echo json_encode($_POST);

Then, since the response will be JSON, you can access the values by key/property in your $.ajax().success() callback function:

...
success: function(result) {
    console.log(result.name) // will log the value stored in 'name'
},
error: function(xhr, textStatus, errorThrown) {
    console.log('Status: ' + textStatus, 'Error: ' + errorThrown)
}

I also added additional error handling so you can actually see what's going on should something go amiss. Learn to embrace errors to better understand your program. Generic errors such as "didn't work!", "database connection failed!", and/or "ajax call no worky!" are useless, for obvious reasons. They are merely breakpoints in the code, but offer no valuable feedback as to why they were executed.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Bro. Saved me
0
<?php
$decoded = json_decode($_POST['data'],true);
foreach ($decoded as $value) {
   return[]=$value["name"];
}
echo json_encode($return);
?>

1 Comment

return[] won't work. You're missing the $ in $return[]
0

There is no need to decode the data on php file... dataType: "json" means the data type of response after ajax call is JSON.

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.