Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery submit()基于php脚本的html表单';s返回值_Php_Jquery_Ajax_Forms - Fatal编程技术网

Jquery submit()基于php脚本的html表单';s返回值

Jquery submit()基于php脚本的html表单';s返回值,php,jquery,ajax,forms,Php,Jquery,Ajax,Forms,好的,如果有人能看看这个,我会非常感激的。我正在html表单中实现验证码。验证码是基于php的,所以我需要使用jquery将提交的表单发布到验证码检查脚本中 如果检查正确,php脚本将返回1,如果检查不正确,则返回0 这一切都很好,我遇到的问题是实际提交表单,或者根据php脚本返回的内容阻止表单。我的密码如下: <form id="contactform" action="FormHandler.cgi" method="POST"> <

好的,如果有人能看看这个,我会非常感激的。我正在html表单中实现验证码。验证码是基于php的,所以我需要使用jquery将提交的表单发布到验证码检查脚本中

如果检查正确,php脚本将返回1,如果检查不正确,则返回0

这一切都很好,我遇到的问题是实际提交表单,或者根据php脚本返回的内容阻止表单。我的密码如下:

                <form id="contactform" action="FormHandler.cgi" method="POST">
    <input name="requiredContact" size="25" type="text" />

    <input name="requiredEmailFrom" size="25" type="text" />
    <input name="requiredTelephone" size="18" tabindex="3" />
    <textarea cols="25" name="comments" rows="4"></textarea>
    <select length="2" name="requiredContactMethod" tabindex="5">
    <option selected="" value="Email">Email</option>
    <option value="Telephone">Telephone</option>
    </select>
    <img src="captcha/captcha.php" style="float:none; margin:0px 0px -8px 0px; height:26px;"></img>
    <input type="text" id="CAPTCHA" style="margin-left: -5px; height:26px;">
            <p id="caperror"></p>
    <p><input name="B1" type="submit" value="Submit" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="B2" type="reset" value="Clear" /></p>
</form> 

<script>



$('#contactform').submit(function(){
var cap = $('#CAPTCHA').val();  
cap = 'CAPTCHA=' + cap;

$.ajax({
type: 'POST',
url: 'captcha/capcheck.php',
data: cap,
dataType: "text",
error: postfail,
 success: success
});

return false;  //Temporary, to stop the form no matter what.
});

function success(result){

if(result == 1){
alert('was correct');
return true;
}
else{
alert("error" + result);
return false;
}


}

function postfail(){
alert('post failed');
    return false;
}


</script>
我不擅长javascript中的函数作用域。如果我在success函数中定义了一个变量,我就不能检查表单提交函数中的值,因为它只有一个局部范围。我可以使用链接提交表单,然后调用submit();但是我希望表单能够被那些没有java的人访问


有没有办法将ajax post success函数的结果返回到submit()处理程序的作用域?

尝试在submit按钮上进行ajax调用,而不是在表单submit上单击


如果click调用的返回为true,则调用submit函数。

我建议在
success
函数中调用
$(“#contactform”).submit()
,但这只会再次调用事件处理程序,您将陷入循环

相反,您可以调用表单元素的
submit
函数,如下所示:

$('#contactform').submit(function(){
    var cap = $('#CAPTCHA').val();  
    cap = 'CAPTCHA=' + cap;
    myform = this; // added this line

    $.ajax({
        type: 'POST',
        url: 'captcha/capcheck.php',
        context: myform, //added this line
        data: cap,
        dataType: "text",
        error: postfail,
        success: success
    });

    return false;  //Temporary, to stop the form no matter what.
});

function success(result){
    // because we added the context option, we can now access the form with the "this" keyword
    if(result == 1){
        alert('was correct');
        // $('#contactform').submit() // this will make an infinite loop
        this.submit();  // this will submit the form
        return true;
    }else{
        alert("error" + result);
        return false;
    }
}

除非我遗漏了什么,否则您应该在AJAX调用的success函数中提交表单。表单提交时也不应抛出AJAX调用。在检查结果为真之前,不得以任何方式提交表格。即:

$.ajax(
    //Parameters and stuff
    ).success(function() {
        //Submit the form
        $('#contactform').submit();
    }).fail(function() {
        //Onoes your call has failed. Do not submit le form :(
    });

就“范围界定”而言,这不应该是“范围界定”问题。如果您需要进一步的指导,请告诉我。

我将编写这样的函数,仅在1结果上设置submitForm

$('#contactform').submit(function(){
   var submitForm = False;
   var cap = $('#CAPTCHA').val();  
   cap = 'CAPTCHA=' + cap;

$.ajax({
   type: 'POST',
   url: 'captcha/capcheck.php',
   data: cap,
   dataType: "text",       
   success:function(result){
       if(result == 1){
          submitForm = True;
       }
   }
   });

    return submitForm;  
});

创建一个无限循环。。。这是我第一次尝试的。因为我在submit处理程序中调用了submit,所以它只会无限重复。就像我说的,这个事件不应该附加到submit处理程序。在触发此事件的某个位置附加一个单击事件,然后在成功后触发提交事件。好的,谢谢。我不知道为什么我不想把所有东西都放在点击处理程序中。submit()方法似乎是当时最好的选择。嗯,看起来很有趣,但我无法让它工作?以MoarCodePlz为例,但谢谢你。
$('#contactform').submit(function(){
   var submitForm = False;
   var cap = $('#CAPTCHA').val();  
   cap = 'CAPTCHA=' + cap;

$.ajax({
   type: 'POST',
   url: 'captcha/capcheck.php',
   data: cap,
   dataType: "text",       
   success:function(result){
       if(result == 1){
          submitForm = True;
       }
   }
   });

    return submitForm;  
});