i use this function in javascript
to communicate with the server php script:
ajax = function( url, ready, json=null, method = 'post') {
var response, request = xhr();
request.open( method, url, true );
request.onreadystatechange = function() {
if( request.readyState == 4 ) {
if( typeof ready == 'function' ){
return ready( request );
} else { return JSON.parse( request.responseText );}
}
}
if(json !== null){
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
return request.send( JSON.stringify( json ) );
}else { return request.send( null );}
}
i made the XMLHttpRequest
after i open the connection using post method
.
i also set the setRequestHeader
to parse JSON: ("Content-Type", "application/json;charset=UTF-8");
and i used this function to colect data
from html form
:
formToValues = function (id) {
var i, values = {}, form = document.getElementById(id);
//var fields = form.querySelectorAll('input,textarea,select'),
for (i = 0; i < form.length ;i++) {
if( form.elements[i].name !== "" ){
values[form.elements[i].name] = form.elements[i].value;
}
}
return values;
}
ajax('a.php', function(response){
console.log(response);
}, formToValues("regForm") );
here is also my html form
:
<form id="regForm" action="javascript:;" method="post" />
<p>
<label for="name">Name</label>
<input type="text" name="name" value="xhr" />
<label for="email">Email:</label>
<input type="email" name="email" value="ro@ew.gq" />
<label for="password">Password:</label>
<input type="password" name="password" value="pass" />
<input type="button" value="Search" />
</p>
now i have a liitle problem reciving data in php
script:
a.php
:
<?php
var_dump( $GLOBALS );
$ar= array( "a"=>2,"b"=>3, "c"=>json_decode( $_REQUEST['params'] ));
echo json_encode($ar);
?>
seems like php take my data sended from javascrip as string and store it in HTTP_RAW_POST_DATA
["HTTP_RAW_POST_DATA"]=>
string(51) "{"name":"xhr","email":"ro@ew.gq","password":"pass"}"
"array(7) {
["GLOBALS"]=>
array(7) {
["GLOBALS"]=>
*RECURSION*
["HTTP_RAW_POST_DATA"]=>
string(51) "{"name":"xhr","email":"ro@ew.gq","password":"pass"}"
["_POST"]=>
array(0) {
}
["_GET"]=>
array(0) {
}
["_COOKIE"]=>
array(0) {
}
["_FILES"]=>
array(0) {
}
["_REQUEST"]=>
array(0) {
}
}
now i can get data with php://input
will not be a problem but i am concernet about security and it dos'n sems ok, and it's one more thing to get in consideration : starting with php version 5.6.0. $HTTP_RAW_POST_DATA — Raw POST data is deprecated~ so what should i do?
Tkanks