繁体   English   中英

使用PHP重定向调用Ajax

[英]Ajax call with a PHP Redirect

在成功提交表单后,如何让我的页面重定向?

目前,当提交表单时出现错误,错误会显示在表单上方。

提交表单时没有错误,成功页面显示在表单上方的错误消息容器中(而不是重定向)。

我的Ajax:

function checkform() {

var form = $('form')[0];
var data = new FormData(form);

    $.ajax({
      url: 'upload.php',
      data: data ,
      processData: false,
      contentType: false,
      dataType: 'text',
      cache: false,
      type: 'POST',
      success: function(data){
        $("div#error").html(data).slideDown("fast");

        //Scroll to top of this div - 70px from the top of your view, change this to whatever number you wish
        var destination = $('div#uploadContainer').offset().top - 15;
        $("html:not(:animated),body:not(:animated)").animate({
            scrollTop: destination
        }, 200);
      }
    });
    return false;
}

我的PHP(验证后......)

if (isset($_POST['messageSubmit'])) {
    header('Location: ' . $message_location);
}

if (isset($_POST['drawingSubmit'])) {
    header('Location: ' . $drawing_location);
}

if (isset($_POST['postSubmit'])) {
    header('Location: ' . $other_location);
}

您需要在javascript success函数中重定向页面,这是通过window.location.href = "http://yourNewLocation" 在你的情况下,我会用onSubmit = "return false"替换onSubmit = "checkForm()" ,在输入中你可以添加<input type="submit" name="drawingSubmit" value="Upload now" onClick = "checkForm('drawingSubmit')" /> 现在在checkForm()函数中,您可以将单击input的名称作为参数 - “

 function checkForm(inputName){
    //current code 
       if(inputName == "drawingSubmit"){
          window.location.href = "//wherever you want to redirect the user";
       }
    }

success回调函数中,您必须设置窗口位置href,如下所示:

window.location.href = 'http://example.com/your-new-path'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM