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 se foloseste in <table> pentru a crea celula de tip "header"?
<thead> <th> <td><table><tr>
<th>Title 1</th>
<th>Title 2</th>
</tr></table>
Ce proprietate CSS seteaza distanta dintre randuri?
line-height word-spacing margin.some_class {
line-height: 150%;
}
Care functie deschide o noua fereastra.
alert() confirm() open()document.getElementById("id_button").onclick = function(){
window.open("http://coursesweb.net/");
}
Indicati functia PHP care returneaza un array cu numele fisierelor si directoarelor dintr-un director.
mkdir() scandir() readdir()$ar_dir = scandir("dir_name");
var_export($ar_dir);
Care din urmatoarele forme a verbului "sleep" (a dormi) se foloseste pentru viitor?
sleeping slept will sleepHe will sleep there.
- El va dormi acolo.
Care din urmatoarele forme a verbului "dormir" (a dormi) se foloseste pentru viitor?
dormido dormirá durmiendoÉl dormirá allí.
- El va dormi acolo.