我找到了这段代码,并尝试按照我需要的方式调整它。我真的不了解javascript / jquery,所以我想要一些帮助。
代码:
$(document).ready(function() {
//if submit button is clicked
$('#submit').click(function () {
//Get the data from all the fields
var name = $('input[name=name]');
var email = $('input[name=email]');
var website = $('input[name=website]');
var comment = $('textarea[name=comment]');
//Simple validation to make sure user entered something
//If error found, add hightlight class to the text field
if (name.val()=='') {
name.addClass('hightlight');
return false;
} else name.removeClass('hightlight');
if (email.val()=='') {
email.addClass('hightlight');
return false;
} else email.removeClass('hightlight');
if (comment.val()=='') {
comment.addClass('hightlight');
return false;
} else comment.removeClass('hightlight');
//organize the data properly
var data = 'name=' + name.val() + '&email=' + email.val() + '&website=' +
website.val() + '&comment=' + encodeURIComponent(comment.val());
//show the loading sign
$('.loading').show();
//start the ajax
$.ajax({
//this is the php file that processes the data
url: "process.php",
//GET method is used
type: "POST",
//pass the data
data: data,
//Do not cache the page
cache: false,
//success
success: function (html) {
$('.loading').hide();
//DISPLAY RECEIVED CONTENT FROM "process.php" IN DIV NAMED "done"
}
});
//cancel the submit button default behaviours
return false;
});
});
这是我从成功部分删除的上一个代码:
success: function (html) {
//if process.php returned 1/true (send mail success)
if (html==1) {
//hide the form
$('.form').fadeOut('slow');
//show the success message
$('.done').fadeIn('slow');
//if process.php returned 0/false (send mail failed)
} else alert('Sorry, unexpected error. Please try again later.');
}
获取信息后,process.php将响应“1”,表单将淡出,div“done”中的文本将淡入。
process.php基本上是:(只是发送回收到的数据用于测试目的)
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$website = ($_GET['website']) ?$_GET['website'] : $_POST['website'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];
echo "$name<br>$email";
就像我说的我对javascript,jquery一无所知,所以任何帮助都会受到赞赏。
感谢。
答案 0 :(得分:1)
我不确定因为您使用的是php
但我认为id更可能是:
if (html=="1") {
在你的成功函数中
但是你的Php似乎返回的不是1:
echo "$name<br>$email";
答案 1 :(得分:1)
如果您正在接收HTML,则可以
success: function (html) {
$('.loading').hide();
$(".done").html(html).fadeIn('slow');
}
我认为你需要回应像
这样的东西echo $name."<br/>".$email;
重置表单,假设myform
是表单的ID
$(':input','#myform')
.not(':button, :submit, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');