Curs Ajax

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
Ce atribut e folosit in tag-ul <a> pentru adresa link-ului?
src href rel
<a href="http://coursesweb.net/" title="CoursesWeb.net">CoursesWeb.net</a>
Ce proprietate CSS seteaza tipul fontului utilizat?
font-family text-decoration font-size
h2 {
  font-family:"Calibri", sans-serif;
}
Ce instructiune selecteaza toate tag-urile <div> cu class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // numar elemente selectate
alert(nr_elms);
Indicati functia care returneaza suma valorilor dintr-un array.
array_sum() array_diff() array_shift()
$arr =[1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10
Indicati forma corecta a verbului "study" (a studia) la Trecutul Simplu in aceasta propozitie: "We (study) English."
studyed studys studied
We studied English.
- Noi am studiat engleza.
Indicati forma corecta a verbului "decir" (a spune) la Prezent in aceasta propozitie: "Yo (decir) siempre la verdad."
digas digo decir
Yo digo siempre la verdad.
- Eu spun mereu adevarul.
Preluare sir JSON in PHP de la JavaScript

Last accessed pages

  1. Rechizite si obiecte scolare (1425)
  2. Curs si Tutoriale JavaScript (92022)
  3. Trecut perfect - Exercitii si teste incepatori (4904)
  4. MySQL JOIN, INNER, LEFT, RIGHT (2395)
  5. Comparatii de inegalitate (1101)

Popular pages this month

  1. Cursuri si Tutoriale: Engleza, Spaniola, HTML, CSS, Php-Mysql, JavaScript, Ajax (2875)
  2. Curs HTML gratuit Tutoriale HTML5 (2213)
  3. Curs si Tutoriale JavaScript (2125)
  4. Curs PHP MySQL, Tutoriale si Scripturi PHP (2064)
  5. Curs CSS Online Tutoriale CSS3 (1900)