问题描述
在成功提交表单后,如何重定向到PHP中的另一个页面?现在,我的代码在提交"按钮下显示绿色消息:您的消息已成功提交!
How can I redirect to another page in PHP upon successful form submission? Right now I have the code that shows a Green Message under Submit button: Your message was successfully submitted!
我想重定向到success.php,并在重新提交php时出错
I'd like to redirect to success.php and in case of an error to resubmit.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文件:
I'm getting an error message here: Uncaught type error: Cannot read property 'message' of undefined. It shows up under the contactform.addAjaxMessage. Do I need to update that code as well? This is my contact-form.js file:
$(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>');
}
};
推荐答案
由于要通过Ajax进行发布,因此在返回成功或错误后,您应该从客户端重定向.
Since you're POSTing via Ajax, you should redirect from the client-side, after returning success or error.
这篇关于成功后将PHP联系人表单重定向到另一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!