【问题标题】:How to redirect successful form submission to another page in PHP [duplicate]如何将成功的表单提交重定向到PHP中的另一个页面[重复]
【发布时间】:2014-05-05 17:56:58
【问题描述】:

如何在表单提交成功后重定向到 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');

}

我在这里收到一条错误消息:未捕获的类型错误:无法读取未定义的属性“消息”。它显示在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>');
  }
};

【问题讨论】:

标签: php forms redirect


【解决方案1】:

你可以简单的使用header函数:

成功案例:

Header('location:success.php');

错误案例:

Header('location:error.php');

【讨论】:

    【解决方案2】:

    简单!只需像这样使用 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,这意味着 URL 已临时移动。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-14
    • 2012-12-08
    • 2022-01-20
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多