如何在PHP中将成功的表单提交重定向到另一个页面
成功提交表单后,如何重定向到PHP中的其他页面? 现在我有一个代码,在提交按钮下显示一条绿色消息:您的消息已成功提交 我想重定向到success.php,如果出现错误,请重新提交.php如何在PHP中将成功的表单提交重定向到另一个页面,php,forms,redirect,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 m
//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>');
}
};
$(文档).ready(函数(){
$(“#反馈提交”)。单击(函数(){
//清除任何错误
contactForm.clearErrors();
//做一点客户端验证——检查每个字段是否有值,以及电子邮件字段的格式是否正确
var hasErrors=false;
$(“#反馈形式输入,textarea”)。不是(“.optional”)。每个(函数(){
if(!$(this.val()){
hasErrors=true;
contactForm.addError($(this));
}
});
var$email=$(“#email”);
如果(!contactForm.isValidEmail($email.val())){
hasErrors=true;
联系方式。附录($email);
}
var$phone=$(“#phone”);
如果(!contactForm.isValidPhone($phone.val())){
hasErrors=true;
contactForm.addError($phone);
}
//如果有任何错误,返回而不发送电子邮件
如果(有错误){
返回false;
}
//发送反馈电子邮件
$.ajax({
类型:“POST”,
url:“library/sendmail.php”,
数据:$(“#反馈形式”).serialize(),
成功:功能(数据)
{
contactForm.addAjaxMessage(data.message,false);
//获得成功的新验证码
$('#captcha').attr('src','library/securimage/securimage_show.php?'+Math.random());
},
错误:函数(响应)
{
contactForm.addAjaxMessage(response.responseJSON.message,true);
}
});
返回false;
});
});
//名称空间不污染全局名称空间
var contactForm={
iValidEmail:功能(电子邮件){
变量正则表达式=/^([a-zA-Z0-9.+-])+\@([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
返回正则表达式测试(电子邮件);
},
/**
*验证电话号码是否有10位数字。
*
*@param{String}要验证的电话号码
*@return{Boolean}如果电话号码有效
*/
isValidPhone:功能(电话){
电话=电话。替换(/[^0-9]/g',);
返回(phone.length==10);
},
clearErrors:函数(){
$('#emailAlert')。删除();
$('#feedbackForm.help block').hide();
$('#feedbackForm.form group').removeClass('has-error');
},
加法器:函数($input){
$input.sides('.help block').show();
$input.parent('.form group').addClass('has-error');
},
addAjaxMessage:函数(消息,iError){
$(“#反馈提交”)。在(“”+$(“”).text(msg.html()+“”)之后;
}
};
简单!只需像这样使用PHP函数。只需确保将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);
响应代码表示“OK”,表示页面将按预期加载;100%可以。但您可能需要将其更改为,这意味着URL已临时移动。简单!只需像这样使用PHP函数。只需确保将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);
响应代码表示“OK”,表示页面将按预期加载;100%可以。但您可能需要将其更改为,这意味着URL已临时移动。简单!只需像这样使用PHP函数。只需确保将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);
响应代码表示“OK”,表示页面将按预期加载;100%可以。但您可能需要将其更改为,这意味着URL已临时移动。简单!只需像这样使用PHP函数。只需确保将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);
响应代码表示“OK”,表示页面将按预期加载;100%可以。但您可能需要将其更改为,这意味着URL已暂时移动。您可以简单地使用标题功能: 成功案例:
Header('location:success.php');
错误案例:
Header('location:error.php');
您可以简单地使用标题功能: 成功案例:
Header('location:success.php');
错误案例:
Header('location:error.php');
您可以简单地使用标题功能: 成功案例:
Header('location:success.php');
错误案例:
Header('location:error.php');
您可以简单地使用标题功能: 成功案例:
Header('location:success.php');
错误案例:
Header('location:error.php');
使用
标题(“location:yoururl.php”)
最初的海报出于某种奇怪的原因在这里开始了一个关于同一主题的新问题。使用标题(“location:yoururl.php”)
最初的海报出于某种奇怪的原因在这里开始了一个关于同一主题的新问题。使用标题(“location:yoururl.php”)
最初的海报出于某种奇怪的原因在这里开始了一个关于同一主题的新问题。使用标题(“location:yoururl.php”)
最初的海报出于某种奇怪的原因在这里开始了一个关于同一主题的新问题。谢谢现在,为什么原始海报会出现在这里&再次发布相同的问题?:)我把它标记为复制品以反映这个。谢谢!现在,为什么原始海报会出现在这里&再次发布相同的问题?:)我把它标记为复制品以反映这个。谢谢!现在,为什么原始海报会出现在这里&再次发布相同的问题?:)我把它标记为复制品以反映这个。谢谢!现在为什么原来的海报会出现在这里