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";
           }
        }
    
    评论

报告相同问题?