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 face ca valoarea din caseta de text sa nu poata fi modificata de utilizator?
checked="checked" readonly="readonly" disabled="disabled"
<input type="text" value="valoare-fixa" readonly="readonly" name="a_name" />
Ce proprietate CSS permite crearea de colturi rotunjite?
background-size border-size border-radius
.class {
  border:2px solid blue;
  border-radius:1.2em;
}
Ce instructiune afiseaza o fereastra cu mesaj si butoane OK si Cancel?
indexOf() confirm() prompt()
var ques = window.confirm("Rezultatul lui 0+0 este 0?");
if (ques) alert("Corect");
else alert("Incorect");
Indicati functia PHP care returneaza numarul cel mai mic dintr-o multime de numere.
floor() ceil() min()
$min_nr = min(12, 8, 25, 13);
echo $min_nr;        // 8
Alegeti verbul corect care trebuie in propozitia: "Its ears ... big".
are is has
Its ears are big.
- Urechile lui sunt mari.
Alegeti verbul corect care trebuie in propozitia: "Los niños ... deportistas"
soy son está
Los niños son deportistas.
- Copii sunt sportivi.
Preluare sir JSON in PHP de la JavaScript

Last accessed pages

  1. Negatia (2347)
  2. Lectii audio-video de initiere in limba engleza (4213)
  3. Formatare text in pagina html (35053)
  4. Curs complet de limba engleza, incepatori (13792)
  5. Lectii audio-video de limba engleza, animate (6079)

Popular pages this month

  1. Exercitii engleza - English Tests and exercises - Grammar (163)
  2. Cursuri si Tutoriale: Engleza, Spaniola, HTML, CSS, Php-Mysql, JavaScript, Ajax (127)
  3. Conditional IF in Limba Engleza - Fraze Conditionale (107)
  4. Pronumele in limba engleza - Pronouns (89)
  5. Gramatica limbii engleze - Prezentare Generala (79)