Home json Simple working example of json, ajax, php communication

Simple working example of json, ajax, php communication

Author

Date

Category

Tell me a simple working example of these entities communicating (json (file) ajax php).

& lt; html & gt;
& lt; head & gt;
& lt; script type = "text / javascript" src = "https://ajax.googleapis.com/ajax/libs
jquery / 1.4.3 / jquery.min.js "& gt; & lt; / script & gt;
& lt; / head & gt;
& lt; body & gt;
& lt; script type = "text / javascript" & gt;
$ (document) .keyup (function () {
  $ .ajax ({
url: 'json.php',
type: 'POST',
data: {'data': JSON.stringify (["Apple", "Orange", "Plum"])}
});
& lt; / script & gt;
& lt; form action = "" & gt; & lt; input type = "search" id = "search-text" & gt; & lt; input type = "submit" & gt; & lt; / form & gt;
& lt; / body & gt;
& lt; / html & gt;

php

var_dump (json_decode ($ _ POST ['data']));

Answer 1, authority 100%

& lt;? php
if (isset ($ _ POST) & amp; & amp;! empty ($ _ POST ['data'])) {
  var_dump (json_decode ($ _ POST ['data']));
  die ();
}
? & gt;
& lt; html & gt;
& lt; head & gt;
& lt; script type = "text / javascript" src = "http://code.jquery.com/jquery-1.8.2.min.js" & gt; & lt; / script & gt;
& lt; / head & gt;
& lt; body & gt;
& lt; script type = "text / javascript" & gt;
$ (document) .ready (function () {
  $ .ajax ({
    url: 'index.php',
    type: 'POST',
    data: {'data': JSON.stringify (["Apple", "Orange", "Plum"])}
  });
});
& lt; / script & gt;
& lt; form action = "" & gt; & lt; input type = "search" id = "search-text" & gt; & lt; input type = "submit" & gt; & lt; / form & gt;
& lt; / body & gt;
& lt; / html & gt;

Answer 2, authority 100%

$. ajax ({
  type: "POST",
  url: "/ ajax / order",
  data: {id: '123123'}
  success: function (html) {
var res = JSON.parse (html);
    alert (html); // just output the response
    alert (res); // parse the JSON
  }
 });

/ajax/order.php

$ options = array (
"status" = & gt; 1,
"err" = & gt; $ _ POST ['id'],
 );
echo json_encode ($ options);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions