语法错误<! - ?php // login.php - >

时间:2013-10-10 13:30:15

标签: javascript php jquery ajax

我再次为大家提供了一些简单的东西,但我得到了

syntax error <?php // login.php line one.

然后在javascript上我得到了

ReferenceError: ajax is not defined 
ajax.onreadyState = function(){  

我看了很久,无法弄明白。我知道它只是愚蠢的我没有放入,但对于我的生活,我不知道。如果你不能告诉我我是新的这一点大声笑。

Html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Login</title>
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
    <body>
        <form action="login.php" method="post" id="loginForm">
            <fieldset>
                <legend>Login</legend>
        <div><label for="email">Email Address</label><input type="email" name="email" id="email" required></div>
        <div><label for="password">Password</label><input type="password" name="password" id="password" required></div> 
        <div><label for="submit"></label><input type="submit" value="Login &rarr;" id="submit"></div>   
            </fieldset>
        </form>
        <script src="js/login.js"></script>
         <script src="resources/login.php"></script>
         <script src="js/ajax.php"></script>
    </body>
    </html>

login.js

function validateForm() {
    'use strict';
    var email = document.getElementById('email');
    var password = document.getElementById('password');
    if ( (email.value.length > 0) && (password.value.length > 0)) {
        var ajax = getXMLHttpRequestObject();
        return true;
    } else {
        alert('Please complete the form!');
        return false;
    }
}
function init() {
    'use strict';
    if (document && document.getElementById) {
        var loginForm = document.getElementById('loginForm');
        loginForm.onsubmit = validateForm;
    }
}
window.onload = init;
ajax.onreadyState = function(){
    if (ajax.readyState == 4) {
        if ( (ajax.status > 200 && ajax.status < 300)
        || (ajax.status == 304) ) {
        if (ajax.responseText == 'VALID') {
            alert('You are now logged in!');
        } else {
            alert('The submitted values do not match those on file!');
        }
    } else {
        document.getElementById('theForm').submit();
    } // End of status IF-ELSE.
} // End of readyState IF.
ajax.open('POST', 'resources/login.php', true);
ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
var data = 'email=' + encodeURIComponent(email.value) + '&password=' + encodeURIComponent(password.value);
ajax.send(data);
return false;
}; // End of function assignation.`

的login.php

<?php // login.php
if ( isset($_POST['email'], $_POST['password'])
&& ($_POST['email'] == 'test@example.com')
&& ($_POST)['password'] == 'securepass') ) {
    ehco 'VALID';
} else {
    echo 'INVALID';
}
?>

4 个答案:

答案 0 :(得分:1)

您的语法错误来自此行:

<script src="resources/login.php"></script>

当你加载它时,你的浏览器将会期待javascript并且只会回复INVALID,这当然不是有效的javascript。注释掉该行并修复其他答案中指出的其他拼写错误,它可能只是运行!

答案 1 :(得分:0)

if (isset($_POST['email'], $_POST['password'])
&& ($_POST['email'] == 'test@example.com')
&& ($_POST['password'] == 'securepass')) {
         ^^^ you have one extra closing bracket here

并且ehco 'VALID';拼写错误,这应该是echo 'VALID';

检查上面的代码并替换你的代码。

答案 2 :(得分:0)

ehcoecho ehco 'VALID';而不是&& ($_POST)['password'] == 'securepass') )此行错误。应该是:&& ($_POST['password'] == 'securepass') )

答案 3 :(得分:0)

您的变量ajax是在localscope中定义的,在您使用它时不可用。并且没有onreadyState事件。