我有一个php邮件表单。如果电子邮件地址未验证,我的jquery将使用成功消息更新emailform div。如果表单成功提交(发布),我只希望显示成功消息。
jquery的:
$('#submit').submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
url: $(this).attr('action'), // the file to call
success: function(response) { // on success..
console.log(response);
if(response != 'error; you need to submit the form!'){
$('#emailform').html("<h2 style='text-align:center;'>Thank you!</h2><hr><p style='text-align:center;'>Thank you for submitting your purchase information.<br>We will send your free gifts soon!</p>"); // update the DIV
}
}
});
return false; // cancel original event to prevent form submitting
});
PHP:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$email = $_POST['email'];
$purchasecode = $_POST['purchasecode'];
$vendor = $_POST['vendor'];
//Validate first
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['purchasecode']) ||
empty($_POST['vendor']))
{
echo "All fields are required.";
exit;
}
if(IsInjected($email))
{
echo "Bad email value!";
exit();
}
$email_from = $email;
$email_subject = "GDFY Purchase Confirmation";
$email_body = "New purchase confirmation from $name.\n".
"Here are the details:\n\n Name: $name \n\n Email: $email \n\n Purchase Code: $purchasecode \n\n Vendor: $vendor";
$to = "idc615@gmail.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email_from \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: index.html');
// echo "success";
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
答案 0 :(得分:0)
好的,我有一些适合你的东西,但是你需要先下载两个文件,这是我从那里获得JS / demo /教程的地方。
来源:http://webdesign.torn.be/tutorials/javascript/prototype/forms-with-prototype/
1)http://webdesign.torn.be/tutorials/assets/images/loading_indicator.gif
2)http://webdesign.torn.be/tutorials/assets/js/prototype.js
另外:
<button type="submit" name="submit" id="submitButton" value="Submit" class="mainButton">SUBMIT</button>
应该改用:
<input type="submit" name="submit" id="submitButton" value="Submit" class="mainButton">
如果必须使用jQuery,请尝试以下操作:
<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script>
jQuery.noConflict();
或者
<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script>
// Give $ back to prototype.js; create new alias to jQuery.
var $jq = jQuery.noConflict();
</script>
<!DOCTYPE html>
<head>
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript">
// <![CDATA[
document.observe('dom:loaded', function() {
function sendForm(event){
// we stop the default submit behaviour
Event.stop(event);
var oOptions = {
method: "POST",
parameters: Form.serialize("contactForm"),
asynchronous: true,
onFailure: function (oXHR) {
$('feedback').update(oXHR.statusText);
},
onLoading: function (oXHR) {
$('feedback').update('Sending data ... <img src="loading_indicator.gif" title="Loading..." alt="Loading..." border="0" />');
},
onSuccess: function(oXHR) {
$('feedback').update(oXHR.responseText);
}
};
var oRequest = new Ajax.Updater({success: oOptions.onSuccess.bindAsEventListener(oOptions)}, "mail_form.php", oOptions);
}
Event.observe('submitButton', 'click', sendForm, false);
});
// ]]>
</script>
</head>
<body>
<div id="feedback"> </div>
<div id="contact">
<h2>Confirm your purchase information</h2>
<hr>
<form method="post" id="contactForm" name="contactform" action="">
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email">
</p>
<p>
<label for='purchasecode'>Purchase Code:</label> <br>
<input type="text" name="purchasecode">
</p>
<p>
<label for='vendor'>Vendor Name:</label> <br>
<select name="vendor">
<option value="1" selected="selected"></option>
<option value="2" >Amazon</option>
<option value="3" >Barnes & Noble</option>
<option value="4" >Family Christian</option>
<option value="5" >Christianbook.com</option>
<option value="6" >LifeWay</option>
<option value="7" >Books-A-Million</option>
<option value="8" >Mardel</option>
</select>
</p>
<input type="submit" name="submit" id="submitButton" value="Submit" class="mainButton"><br>
</form>
</div>
</body>
</html>
注意:将$to = "email@example.com";
更改为您的电子邮件地址
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "Error, you need to submit the form!";
}
$name = $_POST['name'];
$email = $_POST['email'];
$purchasecode = $_POST['purchasecode'];
$vendor = $_POST['vendor'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
die("The email <b>$email</b> you entered, is not valid.");
exit;
}
//Validate first
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['purchasecode']) ||
empty($_POST['vendor']))
{
echo "All fields are required.";
exit;
}
if(IsInjected($email))
{
echo "Bad email value!";
exit();
}
$email_from = $email;
$email_subject = "GDFY Purchase Confirmation";
$email_body = "New purchase confirmation from $name.\n".
"Here are the details:\n\n Name: $name \n\n Email: $email \n\n Purchase Code: $purchasecode \n\n Vendor: $vendor";
$to = "email@example.com"; //<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email_from \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
// header('Location: index.html');
echo "Success";
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>