Acest tutorial explica cu exemple de cod cum se poate prelua in PHP un sir JSON transmis cu Ajax din JavaScript.
Se poate folosi unul din urmatoarele doua variante:

Folosind php://input stream

- Documentatie despre PHP I/O streams
In acest caz obiectul JSON e trimis ca sir la php prin ajax cu Content-type application/json.
- Exemplu.
In JavaScript:
<script>
var data ={s1:'marplo.net', s2:'gamv.eu', y:2020};

//jsn_str = json string
function ajaxF(jsn_str) {
 var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); // XMLHttpRequest object

 request.open('POST', 'test.php', true); // set the request

 //sends data as json
 request.setRequestHeader('Content-type', 'application/json');
 request.send(jsn_str);

 // Check request status
 // If the response is received completely, alert response
 request.onreadystatechange =()=>{
 if(request.readyState ==4){
 alert(request.responseText); // marplo.net
 }
 }
}

//converts data object in json string and sends it to php
let jsn = JSON.stringify(data);
ajaxF(jsn);
</script>
In PHP (test.php):
$arr = json_decode(file_get_contents('php://input'), true);
echo $arr['s1'];
exit;

Adaugare sir JSON in variabila $_POST

In acest caz sirul JSON e atasat la un nume (aici "jsn"), si trimis prin ajax cu POST si Content-type application/x-www-form-urlencoded.
- Exemplu.
In JavaScript:
<script>
var data ={s1:'marplo.net', s2:'gamv.eu', y:2020};

//jsn_str = json string
function ajaxF(jsn_str) {
 var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); // XMLHttpRequest object

 request.open('POST', 'test.php', true); // set the request

 //sends data
 request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
 request.send('jsn='+jsn_str);

 // Check request status
 // If the response is received completely, alert response
 request.onreadystatechange =()=>{
 if(request.readyState ==4){
 alert(request.responseText); // gamv.eu
 }
 }
}

//converts data object in json string and sends it to php
let jsn = JSON.stringify(data);
ajaxF(jsn);
</script>
In PHP (test.php):
$arr = isset($_POST['jsn']) ? json_decode($_POST['jsn'], true) :['s2'=>'default'];
echo $arr['s2'];
exit;

Un Test simplu in fiecare zi

HTML
CSS
JavaScript
PHP-MySQL
Engleza
Spaniola
Care tag HTML5 adauga o aplicatie externa (SWF, PDF) in pagina web?
<mark> <embed> <canvas>
<embed src="flash_game.swf" width="450" height="350" />
Ce pseudo-element adauga un anume stil la prima linie de text din element?
:first-letter :before :first-line
#id:first-line {
  font-weight: bold;
  color: blue;
}
Clic pe proprietatea obiectului window care preia sau seteaza adresa URL a paginii curente.
window.location window.self window.status
var url = window.location;
alert(url);
Indicati functia PHP care preia continutul unui fisier sau pagina si-l adauga intr-un sir.
fopen() file_put_contents() file_get_contents()
$homepage = file_get_contents("http://www.marplo.net/");
echo $homepage;
Care din urmatoarele perechi de numere sunt in ordine unul dupa altu?
seven - eight ten - six three - five
This fruit has seven or eight seeds.
- Acest fruct are sapte sau opt seminte.
Care din urmatoarele perechi de numere sunt in ordine unul dupa altu?
diez - seis siete - ocho tres - cinco
Esta fruta tiene siete u ocho semillas.
- Acest fruct are sapte sau opt seminte.
Preluare sir JSON in PHP de la JavaScript

Last accessed pages

  1. Forma verbului Hay (2168)
  2. Trecutul simplu si continuu - Past Tense Simple and Continuous (39630)
  3. Conditional IF in Limba Engleza - Fraze Conditionale (42560)
  4. Sufixele -ful, -less si -ness - ful, less and ness suffixes (6061)
  5. Creare si editare pagini HTML (31496)

Popular pages this month

  1. Gramatica limbii engleze - Prezentare Generala (2224)
  2. Prezentul simplu si continuu - Present Tense Simple and Continuous (2003)
  3. Trecutul simplu si continuu - Past Tense Simple and Continuous (1859)
  4. Prezent perfect si Prezent perfect continuu - Present perfect and Continuous (1704)
  5. Conditional IF in Limba Engleza - Fraze Conditionale (1358)