weixin_33726318 2016-05-05 09:46 采纳率: 0%
浏览 73

使用PHP重定向进行Ajax调用

How would I get my page to redirect when the form is submitted successfully?

Currently when the form is submitted with an error, the error appears above the form.

When the form is submitted without an error, the success page is shown INSIDE the error message container above the form (instead of redirecting).

My 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;
}

My PHP (after the validation etc...)

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

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

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

2条回答 默认 最新

  • weixin_33719619 2016-05-05 09:50
    关注

    You need to redirect your page in your javascript success function, that is done with window.location.href = "http://yourNewLocation". In your case I would substitute the onSubmit = "checkForm()" with onSubmit = "return false" and in your input you can add <input type="submit" name="drawingSubmit" value="Upload now" onClick = "checkForm('drawingSubmit')" />. Now in your checkForm() function you can get the name of the clicked input as a parameter - `

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

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3