成功后,将PHP联系表单重定向到另一个页面

时间:2014-05-05 18:20:42

标签: php form-submit

如果成功提交表单,如何在PHP中重定向到另一个页面?现在我的代码在“提交”按钮下显示绿色消息:您的消息已成功提交!

我想重定向到success.php,如果出现错误,请重新提交.php

//try to send a message     
if(mail(MY_EMAIL, EMAIL_SUBJECT, setMessageBody($fields_req), "From: $email")) {
echo json_encode(array('message' => 'Your message was successfully submitted.'));
} else {
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(array('message' => 'Unexpected error while attempting to send e- mail.'));
}

当我使用时:

{
header('location: success.php');
}
else {
header('location: resubmit.php');
}

我在这里收到错误消息:未捕获类型错误:无法读取属性' message'未定义的。它显示在contactform.addAjaxMessage下。我是否还需要更新该代码?这是我的contact-form.js文件:

    $(document).ready(function() {
    $("#feedbackSubmit").click(function() {
    //clear any errors
    contactForm.clearErrors();

    //do a little client-side validation -- check that each field has a value and e-mail field is in proper format
    var hasErrors = false;
    $('#feedbackForm input,textarea').not('.optional').each(function() {
      if (!$(this).val()) {
        hasErrors = true;
        contactForm.addError($(this));
      }
    });
    var $email = $('#email');
    if (!contactForm.isValidEmail($email.val())) {
      hasErrors = true;
      contactForm.addError($email);
    }

    var $phone = $('#phone');
    if (!contactForm.isValidPhone($phone.val())) {
      hasErrors = true;
      contactForm.addError($phone);
    }

    //if there are any errors return without sending e-mail
    if (hasErrors) {
      return false;
    }

    //send the feedback e-mail
    $.ajax({
      type: "POST",
      url: "library/sendmail.php",
      data: $("#feedbackForm").serialize(),
      success: function(data)
      {
        contactForm.addAjaxMessage(data.message, false);
        //get new Captcha on success
        $('#captcha').attr('src', 'library/securimage/securimage_show.php?' + Math.random());
      },
      error: function(response)
      {
        contactForm.addAjaxMessage(response.responseJSON.message, true);
      }
   });
    return false;
  });
});

    //namespace as not to pollute global namespace
    var contactForm = {
  isValidEmail: function (email) {
    var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return regex.test(email);
  },
  /**
   * Validates that phone number has 10 digits.
   *
   * @param  {String}  phone phone number to validate
   * @return {Boolean} if phone number is valid
   */
  isValidPhone: function (phone) {
    phone = phone.replace(/[^0-9]/g, '');
    return (phone.length === 10);
  },
  clearErrors: function () {
    $('#emailAlert').remove();
    $('#feedbackForm .help-block').hide();
    $('#feedbackForm .form-group').removeClass('has-error');
  },
  addError: function ($input) {
    $input.siblings('.help-block').show();
    $input.parent('.form-group').addClass('has-error');
  },
  addAjaxMessage: function(msg, isError) {
    $("#feedbackSubmit").after('<div id="emailAlert" class="alert alert-' + (isError ? 'danger' : 'success') + '" style="margin-top: 5px;">' + $('<div/>').text(msg).html() + '</div>');
  }
};

3 个答案:

答案 0 :(得分:2)

由于您是通过Ajax进行POST,因此在返回成功或错误后,您应该从客户端重定向。

答案 1 :(得分:1)

我不需要更新php文件。 这是通过编辑第30行周围的js文件来实现的: 需要将成功回调更改为:

//send the feedback e-mail
    $.ajax({
      type: "POST",
      url: "library/sendmail.php",
      data: $("#feedbackForm").serialize(),
      success: function(data)
      {
     //CHANGE TO YOUR REDIRECT URL HERE
      window.location.href = "http://www.redirectgoeshere.com";
      },
      error: function(response)
      {
        contactForm.addAjaxMessage(response.responseJSON.message, true);
      }
   });
    return false;
  });
});

答案 2 :(得分:0)

尝试使用Location:

    {
    header('Location: success.php');
    }
    else {
    header('Location: resubmit.php');
  }