首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >成功后将PHP联系人表单重定向到另一个页面

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

Stack Overflow用户
提问于 2014-05-06 02:20:42
回答 3查看 496关注 0票数 0

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

我想重定向至success.php,并在出现错误时重定向至resubmit.php

代码语言:javascript
运行
复制
//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.'));
}

当我使用:

代码语言:javascript
运行
复制
{
header('location: success.php');
}
else {
header('location: resubmit.php');
}

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

代码语言:javascript
运行
复制
    $(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>');
  }
};
EN

回答 3

Stack Overflow用户

发布于 2014-05-06 02:25:38

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

票数 2
EN

Stack Overflow用户

发布于 2014-05-06 11:08:45

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

代码语言:javascript
运行
复制
//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;
  });
});
票数 1
EN

Stack Overflow用户

发布于 2014-05-06 02:24:33

试试Location:

代码语言:javascript
运行
复制
    {
    header('Location: success.php');
    }
    else {
    header('Location: resubmit.php');
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23479129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档