PHP回发和JavaScript与Captcha无法正常工作

时间:2014-02-24 02:18:18

标签: javascript php

我的表单使用<?php echo $_SERVER['PHP_SELF']; ?>进行回发,并使用onsubmit="return checkform()"进行JavaScript验证。问题是我无法获得回发和JavaScript验证。提交按钮似乎需要name="submit"才能使回发工作,但是当使用JavaScript设置这种方式时它也不会做任何事情。

if (@$_POST['submit'])
{
    // Get post elements
}
<script language="JavaScript">

    var url = 'captcheck.php?code=';
    var captchaOK = 2;  // 2 - not yet checked, 1 - correct, 0 - failed

    function getHTTPObject()
    {
    try {
        req = new XMLHttpRequest();
    } catch (err1)
    {
        try {
            req = new ActiveXObject("Msxml12.XMLHTTP");
        } catch (err2)
        {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (err3)
            {
                req = false;
            }
        }
    }
    return req;
}

var http = getHTTPObject(); // We create the HTTP Object        

function handleHttpResponse() 
{
    if (http.readyState == 4) {
        captchaOK = http.responseText;
        if(captchaOK != 1) 
        {
            alert('The entered code was not correct. Please try again');
            document.form1.code.value='';
            document.form1.code.focus();
            return false;
        }
        document.form1.submit();
    }
}

function checkcode(thecode) 
{
    http.open("GET", url + escape(thecode), true);
    http.onreadystatechange = handleHttpResponse;
    http.send(null);
}



function checkform() 
{




    if(document.form1.code.value=='') 
    {
        alert('Please enter the string from the displayed image');
        document.form1.code.value='';
        document.form1.code.focus();
        return false;
    }
    // Now the Ajax CAPTCHA validation
    checkcode(document.form1.code.value);
    return false;
}
</script>

<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return checkform()">
    <table width="50%" border="0" align="left">
        <tr>
            <td></td>
            <td valign="top"><input type="text" name="password" size="15" maxlength="10"/>    </td>
        </tr>
        <tr>
            <td></td>

        </tr> 

        <tr><td><strong>Enter the code shown in the box</strong></td>
            <td><img src="captcha.php"></td></tr>
        <tr><td>&nbsp;</td>
            <td><input type="text" name="code" size="15" maxlength="10"><br><br></td></tr>

        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" value="Submit" id="create" name="submit" /></td>
        </tr>
    </table>
</form>

0 个答案:

没有答案