I'm trying to implement the function of checking if the email is exists by using ajax, but my code works on my mac with MAMP while they don't work on my windows PC with XAMPP. Here is the register.php file:
<!doctype html>
<html lang="en" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script type="text/javascript">
/**
* Created by PC2 on 15/12/2016.
*/
/** Create xmlHttpRequest Object */
function getXmlHttpObject() {
var xmlHttpRequest;
if(window.ActiveXObject){ //if the user's web browser is IE core
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttpRequest = new XMLHttpRequest();
}
return xmlHttpRequest;
}
var myXmlHttpRequest = "";
/** Check if the email address is exist or not */
function checkEmail() {
myXmlHttpRequest = getXmlHttpObject();
if(myXmlHttpRequest){
var url = "/Ajax-Check-Username-Clean-Code/Ajax/registerProcess.php";
var data = "email="+$('email').value;
myXmlHttpRequest.open("POST",url,true);
myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
myXmlHttpRequest.onreadystatechange = process; //process is a function
myXmlHttpRequest.send(data);
}else{
window.alert("Create Ajax Engine Fail!");
}
}
/** function to change the visibility of the div */
function process() {
if(myXmlHttpRequest.readyState == 4){
var mes = myXmlHttpRequest.responseText;
var mes_obj = eval("(" + mes + ")");
var mes_status = mes_obj.status;
}else{
window.alert("Error");
}
if(mes_status == "ok"){
$('message').textContent = "ok";
}else if(mes_status == "error"){
$('message').textContent = "Error";
}
}
function $(id) {
return document.getElementById(id);
}
</script>
<title>Check Username</title>
</head>
<body>
<section>
<form class="register-form">
<div class="register">
<h2>Email:</h2><input type="text" id="email" name="email-input" onkeyup="checkEmail();"><div class="text" id = "message"></div>
<h2>Password</h2><input type="password" class="password-input"><br/><br/>
<button class="submit">Sign Up</button>
</div>
</form>
</section>
</body>
</html>
And Here is the PHP file:
<?php
$email = $_POST['email'];
$mes = "";
if ($email == "songtao"){
$mes = '{"status":"error"}';
} else {
$mes = '{"status":"ok"}';
}
echo $mes;
?>
These code works well on my Mac, when I tried to run the code on windows, there is an error said "undefined index" in the php file ( $email = $_POST['email'] ), anyone has any ideas why this happened?