PHP脚本不会将内容传递给Ajax调用

时间:2015-11-12 21:37:38

标签: php html ajax xml xhtml

我想知道我的代码有什么问题,它是一个非常简单的ajax调用php脚本,但php脚本中的文本不会被传递到html页面。有什么问题?

<html>
<script>
function checkForm() {

    var pass = document.getElementById("oldpassword").value;
    var pass1 = document.getElementById("passwordnew1").value;
    var pass2 = document.getElementById("passwordnew2").value;

    if (pass == '' || pass1 == '' || pass2 == '') {
        alert("Fill All Fields");
    } else {

        var passcheck = document.getElementById("editpassword");
        var passcheck1 = document.getElementById("equalpasswords1");
        var passcheck2 = document.getElementById("equalpasswords2");

        if (passcheck.innerHTML == 'Must be 3+ letters' || passcheck2.innerHTML == 'Password too short' || passcheck3.innerHTML == 'Invalid email') {
            alert("Fill Valid Information");
        } else {
            document.getElementById("myForm").submit();
        }
    }
}


function validate(field, query) {
    var xmlhttp;

    if (window.XMLHttpRequest) { // for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {

    if (xmlhttp.readyState != 4 && xmlhttp.status == 200) {
        document.getElementById(field).innerHTML = "Validating..";
    } else if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById(field).innerHTML = xmlhttp.responseText;
    } else {
        document.getElementById(field).innerHTML = "Error Occurred. <a     href='index.html'>Reload Or Try Again</a> the page.";
    }

    xmlhttp.open("GET", "validation.php?field=" + field + "&query=" + query, false);
    xmlhttp.send();
}
</script>
<body>
    <form action="#" id="myForm" name="passwordform" method="post">
        <div class="container">
            <table>
                <tr>
                    <td style="width:100px"><p><b>My email:</b></td>
                    <td><p><?php print($email); ?></p></td>
                    <td></td>
                </tr>
                <tr>
                    <td><p><b>My password:</b></td>
                    <td><p id="editpassword"></p><p id="insertpassword"><input type="password"     id="oldpassword" name="inputedPassword" onblur="validate('inputedPassword',  this.value)" value="<?php print($password); ?>"/></p></td>
                    <td></td>
                </tr>
                <tr>
                    <td><p><b>My new password:</b></td>
                    <td><p id="equalpasswords1"></p><input type="password" id="passwordnew1"  name="Password1" onblur="validate('Password1', this.value)"/></td>
                    <td></td>
                </tr>
                <tr>
                    <td><p><b>Confirm password:</b></td>
                    <td><p id="equalpasswords2"></p><input type="password" id="passwordnew2"    name="Password2" onblur="validate('Password2', this.value)"/> <input  type="button" value="Save" onclick="checkForm()"></td>
                    <td></td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

这是它所调用的PHP代码:

<?php
$value = $_GET['query'];
$formfield = $_GET['field'];

if ($formfield == "inputedPassword") {
    if (strlen($value) < 4) {
        echo "Must be 3+ letters";
    } else {
        echo "<span>Valid</span>";
    }
}

if ($formfield == "Password1") {
    if (strlen($value) < 6) {
        echo "Password too short";
    } else {
        echo "<span>Strong</span>";
    }
}

if ($formfield == "Password2") {
    if (strlen($value) < 6) {
        echo "Password too short";
    } else {
        echo "<span>Strong</span>";
    }
}

?>

1 个答案:

答案 0 :(得分:0)

var passcheck = document.getElementById("editpassword");
var passcheck1 = document.getElementById("equalpasswords1");
var passcheck2 = document.getElementById("equalpasswords2");

if (passcheck.innerHTML == 'Must be 3+ letters' || passcheck2.innerHTML == 'Password too short' || passcheck3.innerHTML == 'Invalid email') {

if测试中的变量名称不正确。您已声明变量passcheckpasscheck1passcheck2,但在if测试中,您正在调用passcheckpasscheck2passcheck3