如何在PHP中将成功的表单提交重定向到另一个页面

时间:2014-05-05 17:56:58

标签: php forms redirect

如果成功提交表单,如何在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>');
  }
};

2 个答案:

答案 0 :(得分:2)

您可以简单地使用标题功能:

成功案例:

Header('location:success.php');

错误案例:

Header('location:error.php');

答案 1 :(得分:1)

轻松!只需使用PHP header函数即可。请务必将success.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.'));
  header('Location: success.php');
  exit;
} else {
  header('HTTP/1.1 500 Internal Server Error');
  echo json_encode(array('message' => 'Unexpected error while attempting to send e-mail.'));
}

您还可以通过更改此设置来设置特定的HTTP响应代码:

header('Location: success.php');

对此:

header('Location: success.php', false, 200);

200响应代码表示“OK”,表示页面将按预期加载; 100%好的。但您可能希望将其更改为302,这意味着该网址已暂时移动。