Javascript 如何消除联系人表单的ajax功能?
是的,这是我买的登录页: 我要你试着注册 注册后出现的成功和错误消息显示在同一页面中,具有相同的URL 我认为这就是ajax函数,对吗 我希望保留错误消息的ajax功能,但我希望成功消息出现在一个全新的页面中,带有一个全新的url,比如:contact-thank-you.html 我已经创建了html感谢页面,但我不知道如何在人们成功提交后指导他们 有什么想法吗 求你了,求你了 :D 这是我的js代码,它位于一个名为custom.js的文档中:Javascript 如何消除联系人表单的ajax功能?,javascript,php,ajax,contact-form,Javascript,Php,Ajax,Contact Form,是的,这是我买的登录页: 我要你试着注册 注册后出现的成功和错误消息显示在同一页面中,具有相同的URL 我认为这就是ajax函数,对吗 我希望保留错误消息的ajax功能,但我希望成功消息出现在一个全新的页面中,带有一个全新的url,比如:contact-thank-you.html 我已经创建了html感谢页面,但我不知道如何在人们成功提交后指导他们 有什么想法吗 求你了,求你了 :D 这是我的js代码,它位于一个名为custom.js的文档中: // *********************
// ********************************
// Request and contact Form
// ********************************
$("#formIndex, #contact").submit(function() {
var elem = $(this);
var urlTarget = $(this).attr("action");
$.ajax({
type : "POST",
url : urlTarget,
dataType : "html",
data : $(this).serialize(),
beforeSend : function() {
elem.prepend("<div class='loading alert'>" + "<a class='close' data-dismiss='alert'>×</a>" + "Loading" + "</div>");
},
success : function(response) {
elem.prepend(response);
elem.find(".loading").hide();
elem.find("input[type='text'],input[type='email'],textarea").val("");
}
});
return false;
});
//********************************
//申请及联络表格
// ********************************
$(“#formIndex,#contact”).submit(函数(){
var elem=$(本);
var urlTarget=$(this.attr(“操作”);
$.ajax({
类型:“POST”,
url:urlTarget,
数据类型:“html”,
数据:$(this).serialize(),
beforeSend:function(){
元素前置(“+”ד+”加载“+”);
},
成功:功能(响应){
元素前置(应答);
元素find(“.loading”).hide();
元素查找(“输入[type='text'],输入[type='email'],textarea”).val(“”);
}
});
返回false;
});
以下是我的php代码,它位于名为contact-form.php的文档中:
<?php
$to="company.email";/*Your Email*/
$subject="Message from the Landing - Contact Form ";
$date=date("l, F jS, Y");
$time=date("h:i A");
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$phone=$_REQUEST['phone'];
$caoch=$_REQUEST['caoch'];
$file=$_REQUEST['file'];
$msg="
Message sent from Landing Contact Form on date $date, hour: $time.\n
First Name: $name\n
Email: $email\n
Phone: $phone\n
Caoch: $caoch\n
File: $file
";
if($email=="") {
echo "<div class='alert alert-danger'>
<a class='close' data-dismiss='alert'>×</a>
<strong>Warning!</strong> Please fill all the fields.
</div>";
} else {
mail($to,$subject,$msg,"From:".$email);
header("class='alert alert-success' Location: contact-thank-you.html");
exit;
}
?>
修改AJAX调用,如下所示:
$('#formIndex, #contact').submit(function() {
var elem = $(this);
var urlTarget = elem.attr('action');
$.ajax({
type: 'POST',
url: urlTarget,
dataType: 'html',
data: elem.serialize(),
beforeSend: function() {
elem.prepend('<div class="loading alert><a class="close" data-dismiss="alert">x</a>Loading</div>');
},
success: function(response) {
response = $(response);
if (response.hasClass('alert-success')) {
location.href = 'URL TO YOUR CONTACT-THANK YOU PAGE';
} else {
elem.prepend(response);
elem.find('.loading').remove();
elem.find('input[type="text"], input[type="email"], textarea').val('');
}
}
});
});
$('#formIndex,#contact')。提交(函数(){
var elem=$(本);
var urlTarget=elem.attr('action');
$.ajax({
键入:“POST”,
url:urlTarget,
数据类型:“html”,
数据:elem.serialize(),
beforeSend:function(){
elem.prepend(“首先,您应该向支持团队询问您现在购买该模板的地方。围绕您的问题,您应该返回一些JSON,并在jQuery代码中利用这些JSON,例如:
$.get('URL').success(function (data) {
if (data.error) {
// Show the error message on the same page and prevent the default behavior
} else {
// then all was fine so this is your success
location.href = 'another'; // where another is the page where you want to show your success message
}
}).error(function (data, status, headers, config) {
if (status == '500') {
console.log("No server connection");
}
});
希望这些帮助谢谢@Amelle!!我必须删除ajax调用中的任何内容吗?我应该保留什么?我应该在下面放什么?谢谢!!!@RiversO不,你不必删除任何内容,只需在我的答案中添加这些行。…
s只需说“这里有更多核心”哈哈:)@Amelle Balane,我在成功后放了代码:函数(响应)段落,但成功消息仍然显示在同一页上,使用ajax,保持相同的url。这是我的登录页:@RiversO Wait让我编辑我的答案以显示整个ajax调用。非常感谢,@Amelle Balance!您应该阅读代码并试着理解它,然后您可以更改默认函数(您发布的函数)如果你复制/粘贴我的代码不起作用,因为这只是一个例子。我是超级新的@this,我还不太懂编码。D:我不在电脑旁,明天回来的时候,如果你不能解决问题,我会尽力帮你完成代码
$.get('URL').success(function (data) {
if (data.error) {
// Show the error message on the same page and prevent the default behavior
} else {
// then all was fine so this is your success
location.href = 'another'; // where another is the page where you want to show your success message
}
}).error(function (data, status, headers, config) {
if (status == '500') {
console.log("No server connection");
}
});