使用ajax中的POST方法将数据从JavaScript传递到PHP时遇到问题

时间:2016-12-15 18:04:05

标签: javascript php ajax

我试图通过使用ajax来实现检查电子邮件是否存在的功能,但我的代码在我的Mac上与MAMP一起工作,而他们不能在我的带有XAMPP的Windows PC上工作。这是register.php文件:



<!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>
&#13;
&#13;
&#13;

这是PHP文件:

&#13;
&#13;
<?php


$email = $_POST['email'];

$mes = "";

if ($email == "songtao"){
    $mes = '{"status":"error"}';
} else {
    $mes = '{"status":"ok"}';
}

echo $mes;


?>
&#13;
&#13;
&#13;

这些代码在我的Mac上运行良好,当我尝试在Windows上运行代码时,会出现一个错误&#34; undefined index&#34;在php文件中($ email = $ _POST [&#39; email&#39;]),任何人都有任何想法为什么会这样?

0 个答案:

没有答案