Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.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
使用php和jquery成功将数据插入数据库后的页面重定向_Php_Jquery - Fatal编程技术网

使用php和jquery成功将数据插入数据库后的页面重定向

使用php和jquery成功将数据插入数据库后的页面重定向,php,jquery,Php,Jquery,我有一个html表单,我使用Jquery用Php验证它。成功地将数据插入数据库后,它将在html页面中显示一条成功消息,并重定向到其他页面。嗯,它工作正常,但如果出现错误,它也会重定向到其他页面 我想要什么: 如果有错误,它应该留在这个页面上。如果没有错误,则应该重定向到其他页面。如何使用jquery实现这一点 <center><img id="loading-image" src="images/loading-image.gif" style="display:none;

我有一个html表单,我使用Jquery用Php验证它。成功地将数据插入数据库后,它将在
html
页面中显示一条成功消息,并重定向到其他页面。嗯,它工作正常,但如果出现错误,它也会重定向到其他页面

我想要什么:
如果有错误,它应该留在这个页面上。如果没有错误,则应该重定向到其他页面。如何使用jquery实现这一点

<center><img id="loading-image" src="images/loading-image.gif" style="display:none; margin-bottom:10px !important;"/></center>
<div id="result"></div>

$('body').on('click', '#request_tutor', function (e) {
    e.preventDefault();

    var formData = new FormData($(this).parents('form')[0]);
    $("#loading-image").show();
    $.ajax({
        url: 'request_tutor_process.php',
        type: 'POST',
        xhr: function () {
            var myXhr = $.ajaxSettings.xhr();
            return myXhr;
        },
        success: function (data) {
            $("#loading-image").hide(); //hide loading          
            $("#result").html(data);

            setTimeout(function () {
                $('input[type=submit]').attr('disabled', false);
                window.location.href = "login/index.php";
            }, 2000);

        },

        complete: function () {
            $("#loading-image").hide(); //hide loading here
        },

        data: formData,
        cache: false,
        contentType: false,
        processData: false
    });
});

$('body')。在('click','request#u tutor',函数(e)上{
e、 预防默认值();
var formData=new formData($(this).parents('form')[0]);
$(“#加载图像”).show();
$.ajax({
url:“request\u tutor\u process.php”,
键入:“POST”,
xhr:函数(){
var myXhr=$.ajaxSettings.xhr();
返回myXhr;
},
成功:功能(数据){
$(“#加载图像”).hide();//隐藏加载
$(“#结果”).html(数据);
setTimeout(函数(){
$('input[type=submit]').attr('disabled',false);
window.location.href=“login/index.php”;
}, 2000);
},
完成:函数(){
$(“#加载图像”).hide();//在此处隐藏加载
},
数据:formData,
cache:false,
contentType:false,
processData:false
});
});
Php页面:

$err =  array();
// required eata validation checking...
if(isset($name) && isset($tutoring_location) && isset($pcode) && isset($mphone) && isset($email) && isset($tutor_level)){
    if(empty($name) && empty($tutoring_location) && empty($pcode) && empty($mphone) && empty($email) && empty($tutor_level)){
        $err[] = "Please fill up required field which is indicated by *";
    }else{
        if(empty($name))    
            $err[] = "Your name required";
        elseif(strlen($name) > 30)
            $err[] = "Your name is too long";

        if(empty($tutoring_location))   
            $err[] = "Write your tutoring location";
        elseif(strlen($tutoring_location) > 255)
            $err[] = "Tutoring location address is too long";

        if(empty($pcode))   
            $err[] = "Postal code required";
        elseif(strlen($pcode) > 6)
            $err[] = "Invalid postal code"; 
        elseif(!is_numeric($pcode))
            $err[] = "Invalid postal code";

        if(empty($mphone))  
            $err[] = "Mobile phone number required";
        elseif(strlen($mphone) > 8)
            $err[] = "Invalid mobile phone number";     
        elseif(!is_numeric($mphone))
            $err[] = "Invalid mobile phone number"; 

        if(empty($email))   
            $err[] = "Your email address required.";    
        elseif(@!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
            $err[] = "Invalid email address";
        elseif($num_email == 1)
            $err[] = "Email address <strong>$email</strong> is already exits, please choose another";       

        if(empty($tutor_level)) 
            $err[] = "Select your tutor level";         
    }

    if(!empty($err)){
        echo "<div class='error'>";
        foreach($err as $er){
            echo "$er. <br/>";
        }
        echo "</div>";
    }else{
        ecoh "success";
    }
}
$err=array();
//必需的eata验证检查。。。
if(isset($name)&&isset($tutoring_location)&&isset($pcode)&&isset($mphone)&&isset($email)&&isset($tutort_level)){
if(空($name)&&empty($tutoring_location)&&empty($pcode)&&empty($mphone)&&empty($email)&&empty($tutort_level)){
$err[]=“请填写*所示的必填字段”;
}否则{
if(空($name))
$err[]=“需要您的姓名”;
埃尔塞夫(斯特伦($name)>30)
$err[]=“您的名字太长”;
if(空($tutoring_location))
$err[]=“编写您的教程位置”;
elseif(斯特伦($tutoring_location)>255)
$err[]=“教程位置地址太长”;
if(空($pcode))
$err[]=“需要邮政编码”;
elseif(strlen($pcode)>6)
$err[]=“无效邮政编码”;
elseif(!是数字($pcode))
$err[]=“无效邮政编码”;
if(空($mphone))
$err[]=“需要手机号码”;
elseif(斯特伦($mphone)>8)
$err[]=“无效的手机号码”;
elseif(!是数字($mphone))
$err[]=“无效的手机号码”;
如果(空($email))
$err[]=“需要您的电子邮件地址。”;
elseif(@!eregi(^[[U a-z0-9-]+(\.[U a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$,$email))
$err[]=“无效的电子邮件地址”;
elseif($num_email==1)
$err[]=“电子邮件地址$Email已退出,请选择另一个”;
if(空($U级))
$err[]=“选择您的导师级别”;
}
如果(!empty($err)){
回声“;
foreach($err为$er){
回声“$er.
”; } 回声“; }否则{ 经济合作组织“成功”; } }
这可能是罪魁祸首

setTimeout(function () {
    $('input[type=submit]').attr('disabled', false);         
    window.location.href = "login/index.php"; 
},   2000); 
代码成功后,将设置一个全局超时,每两秒钟重定向一次页面。出于某种原因,因为您没有显示PHP代码,所以它总是成功的。基本上,只要响应返回为有效(HTTP响应代码200),它就会始终成功。如果PHP端有错误,则需要设置适当的错误头,以便调用jQuery错误函数


一句话:您很可能没有向AJAX函数返回HTTP错误代码,从而导致始终调用success函数。

不要使用前端语言验证表单输入

我看到对'request\u tutor\u process.php'的AJAX调用使用POST,因此request\u tutor\u process.php应该有如下内容:

if($_POST['action'] == 'formSubmit'){
    //Let's say you have this function called 'add_to_database'
    attemptToAdd = add_to_database();
    //Let's say something went wrong with the form validation, and you return 'false'
    if(attemptToAdd === false) echo "Failed to submit, please try again later";
    //Let's say it worked, but it was unsuccessful (the username already exists)
    if(attemptToAdd === "unsuccessful") header('Location: /failed.php');
    //Let's say it worked fully...
    if(attemptToAdd === "successful") echo "It worked, click *here* to continue;
}

你的问题是它总是会进入成功区,对吗

如果是这样,请遵循@Zarathuztra answer并在PHP代码中将HTTP代码设置为200以外的任何值,然后添加一个错误块来处理错误。 参考这个答案

或者,您可以检查数据的值,看看是否有错误。

尝试以下操作:

     success: function (data) {
          $("#loading-image").hide(); //hide loading          
          $("#result").html(data);

        if(data == "success") {
          setTimeout(function () {
            $('input[type=submit]').attr('disabled', false);
            window.location.href = "login/index.php";
          }, 2000);
        } else {
          alert('error');
          return false;
        }

顺便说一下,PHP有点混乱,但这不是问题所在。基本上,因为数据有一个值,所以它是成功的,并且您没有处理错误

如何使用jQuery验证PHP?PHP是服务器端,jQuery是客户端。他们不能直接互动。是否确实要在整个身体上设置单击事件?您的PHP代码是什么样子的?@Zarathuztra在按下提交按钮后,它将使用jquery on click方法调用PHP页面。将数据插入db之后,php将成功消息返回给jquery,即
$(“#result”).html(数据)@John Barca感谢您编辑我的问题。为了用户体验的目的,您实际上应该同时使用前端和服务器端。@1owk3y我更新了我的问题。你可以看到我的php代码。我明白了。谢谢你的链接。