php - 'undefined index' when trying to parse JSON string -


I think there is some syntax error or conceptual error

I have a simple AJAX call where I am passing a JSON string in a PHP file The AJAX call looks like this:

  & lt; Script & gt; $ (Document) .ready (function () {$ ("# btn"). Click (function () {var id = $ ('# id'). Val (); var name = $ ('# name') .val (); var address = $ ('# address'). Val (); $ .ajax ({url: "connection.php", type: "post", data: {topost: "{id:" + ID + "',' name ':'" + name + "',' address ':' + + + + + ''}}", datatype: "JSONP", success: work (status) {if ( Status .success == false) {Warning ("failure!");} And {warning ("success!");}}})}});}); & Lt; / Script & gt;   

In this way I am trying to parse the string in my server-side PHP file:

  & lt ;? Php header ('Content-type: application / json'); $ Server = "localhost"; $ Username = "root"; $ Password = ""; $ Database = "jqueryex"; $ Con = mysql_connect ($ server, $ username, $ password); If ($ con) {resonant "connect to database!"; } Else {resonant "Could not connect!"; } // or die ("could not connect:". Mysql_error ()); Mysql_select_db ($ database, $ conn); $ Thedata = json_decode ($ _ POST ['top']); Echo ($ thedata); Mysql_close ($ thief); ? & Gt; My question is:  

Why do I get an error on the line of 'undefined index topmost' where she says:

  $ thedata = json_decode ($ _ POST ['top']);   

My topost comes from my AJAX, right? So why is my 'undefined' in PHP?

I am just flying from here and this is probably not your problem at all, but when you have content -php does not parse Jason in your body: the application / Jason does not appear in the specified variable $ _POST variable like this

You have to parse your body, which It is not as difficult as it seems .. In this way something simplified by importing Jason body into _POST variable Ega. This is not the safest implementation, and I do not recommend using it without any cleaning:

  $ _POST = array_merge ($ _POST, json_decode (file_get_contents ('php: // input') ,truth));   

Hope it helps.

Comments