Reputation: 13
this is my register.js
var formdata = new FormData();
formdata.append("name", name.value);
formdata.append("username", username.value);
formdata.append("email", email.value);
formdata.append("password", password.value);
var ajax = new XMLHttpRequest();
var response = eval(ajax.responseText);
alert(response);
ajax.open("POST", "/req/register/register.req.php");
ajax.send(formdata);
and there is my register.req.php
require '../../inc/global.inc.php';
ini_set('display_errors', 0);
list($first_name, $last_name) = explode(" ", $_POST['name']);
$db->query("INSERT INTO users (first_name, last_name, username, password, email) VALUES ('%s', '%s', '%s', '%s', '%s')", $first_name, $last_name, $_POST['username'], md5($_POST['password']), $_POST['email']);
header('Content-Type: application/json');
echo json_encode(array('OK'));
And the question is how to get the 'OK' string, because var response = eval(ajax.responseText); gets just an 'undefinied' variable.
Upvotes: 1
Views: 4152
Reputation: 1385
You can get the response in onreadystatechange method.
var ajax = new XMLHttpRequest();
ajax.onreadystatechange=function()
{
if (ajax.readyState==4 && xmlhttp.status==200)
{
var response = ajax.responseText;
alert(response);
}
}
ajax.open("POST", "/req/register/register.req.php");
ajax.send(formdata);
Upvotes: 2