我即将完成第一次创建自定义联系人表单。我已经准备好了一切-所有我需要的是php/ajax代码。经过谷歌搜索,这是我得到的:
输入字段(HTML文件):
<label for="userName">Name:</label>
<input id="userName" class="inputField" type="text" name="userName"/>
<label for="userEmail">E-mail:</label>
<input id="userEmail" class="inputField" type="text" name="userEmail"/>
<label for="userSubject">Subject:</label>
<input id="userSubject" class="inputField" type="text" name="userSubject"/>
<label for="userMessage">Message:</label>
<textarea id="userMessage" class="inputField" rows="" cols="" name="userMessage" style="overflow:hidden"></textarea>
<label for="userSubject">Assunto:</label>
<input id="userSubject" class="inputField" type="text" name="userSubject"/>
我的按钮是一个动画函数(不是 <input type="button"/>
)。
<?php
// Main Variables Used Throughout the Script
$domain = "http://" . $_SERVER["HTTP_HOST"]; // Root Domain - http://example.com
$siteName = "wwwnovaplaquemar.pt";
$siteEmail = "guida.e.pedro@gmail.com";
$er = "";
// Check if the web form has been submitted
if (isset($_POST["userEmail"])){
// Process the web form variables
// Strip out any malicious attempts at code injection, just to be safe.
// All that is stored is safe html entities of code that might be submitted.
$userName = htmlentities(substr($_POST["userName"], 0, 100), ENT_QUOTES);
$userEmail = htmlentities(substr($_POST["userEmail"], 0, 100), ENT_QUOTES);
$userSubject = htmlentities(substr($_POST["userSubject"], 0, 100), ENT_QUOTES);
$userMessage = htmlentities(substr($_POST["userMessage"], 0, 10000), ENT_QUOTES);
// Perform some logic on the form data
// If the form data has been entered incorrectly, return an Error Message
// Prepare the E-mail elements to be sent
$subject = $userSubject;
$message =
'<html>
<head>
<title>' . $siteName . ': A Contact Message</title>
</head>
<body>
' . wordwrap($userMessage, 100) . '
</body>
</html>';
// We are sending the E-mail using PHP's mail function
// To make the E-mail appear more legit, we are adding several key headers
// You can add additional headers later to futher customize the E-mail
$to = $siteName . ' Contact Form <' . $siteEmail . '>';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
// Additional Headers
$headers .= 'From: ' . $userName . ' <' . $userEmail . '>' . "rn";
$headers .= 'Reply-To: ' . $userName . ' <' . $userEmail . '>' . "rn";
$headers .= 'Return-Path: ' . $userName . ' <' . $userEmail . '>' . "rn";
$headers .= 'Date: ' . date("r") . "rn";
$headers .= 'X-Mailer: ' . $siteName . "rn";
// And now, mail it
if (mail($to, $subject, $message, $headers)){
echo '<div>Thank you for contacting ' . $siteName . '. We will read your message and contact you if necessary.</div>';
}
else {
$er .= 'We weren't able to send your message. Please contact ' . $siteEmail . '.<br />';
}
}
?>
在我的js文件中:
$.ajax({
type: "POST",
url: "send_pt.php",
data: data,
success: function(){
$('#inputText_infoBox p').html("A enviar e-mail...");
}
});
问题:我把什么放在哪里?
假设点击事件正在调用ajax函数,您需要使用ajax中的data
向服务器发送数据,使用jquery.serialize序列化表单,这会将所有表单元素发送到服务器
试试这个
$.ajax({
...
data: $('#yourFormID').serialize(),
success: function(){
...