0

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?

  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Qirel Dec 15 '16 at 18:05
  • Why you are not using jquery ajax? – Nitin Dhomse Dec 15 '16 at 18:07
  • I'm assuming your Mac environment and Windows environment have a different level of error reporting. That notice is just saying the $_POST array doesn't contain a key called 'email'. If I was you I would be checking the $_POST array to see if the 'email' key exists before I try and use it. – David Jones Dec 15 '16 at 18:22
  • how you get email field using $('email').value? Instead use document.getElementById('email').value......Your error message is not getting the data field because of the JS error. – BetaDev Dec 15 '16 at 18:48
  • You are not using JQuery that's why you can not use "$('email').value" to get and sent the value of email field. And also inspect the page, go to the console and refresh the page and then see the error msg if any. – BetaDev Dec 15 '16 at 18:58

0 Answers0