我想创建一个非常基本的联系表单。我已经使用我所熟悉的语言知识编写了HTML,Javascript和PHP,但它显然不起作用。有什么遗漏?
HTML:
<form>
<fieldset>
<fieldset>
<input name="name" value="John Doe" size="30" /><br />
<input name="email" value="john@doe.com" size="40" /><br />
</fieldset>
<fieldset>
<input type="text" name="subject" value="Subject" size="60" /><br />
<textarea name="message" rows="15" cols="100"></textarea><br /><br />
<input type="submit" value="Submit" />
<input type="button" value="Cancel" />
</fieldset>
</fieldset>
</form>
Javascript:
$('form').submit( function() {
$.ajax({
type: "POST",
url: "bin/process.php",
data: $(this).serialize(),
success: function() {
// Update page with success message
}
});
return false;
});
PHP:
<?php
$recipient = "me@email.com"; //recipient
$Name = ($_POST['name']); //senders name
$email = ($_POST['email']); //senders e-mail adress
$mail_body = ($_POST['message']); //mail body
$subject = ($_POST['subject']); //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
mail($recipient, $subject, $mail_body, $header); //mail command :)
?>
答案 0 :(得分:0)
已修改问题/评论的更新:
您确定您的代码是在document.ready
处理程序中运行吗?,如下所示:
$(function() {
$('form').submit( function() {
$.ajax({
type: "POST",
url: "bin/process.php",
data: $(this).serialize(),
success: function() {
// Update page with success message
}
});
return false;
});
});
根据您的评论,您的.submit()
handler似乎根本没有被绑定,而且它正在执行默认的GET浏览器行为。