我有一个简单的表单,我正在使用 jquery livevalidate 进行验证,并使用 php 脚本进行处理。我不想在表单成功提交后重定向页面,而是想使用 jquery 显示一个 div(并保持在同一页面上)。
<form action="/process_form.php" method="post">
<div id="field1-container">
<label>Name</label>
<input type="text" name="name" id="name">
<script type="text/javascript">
var f1 = new LiveValidation('name');
f1.add(Validate.Presence);
f1.add( Validate.Length, { minimum: 4, maximum: 30 } );
</script>
</div>
<div id="field4-container" class="field f_100 twelve columns">
<label>
Email Address
</label>
<input type="text" name="email" id="email">
<script type="text/javascript">
var f2 = new LiveValidation('email');
f2.add(Validate.Presence);
f2.add( Validate.Email );
</script>
</div>
<div id="field3-container" class="field f_100 twelve columns">
<label>Message</label>
<textarea rows="5" name="message" id="message"></textarea>
<script type="text/javascript">
var f3 = new LiveValidation('message');
f3.add(Validate.Presence);
f3.add( Validate.Length, { minimum: 20, maximum: 300 } );
</script>
</div>
<br>
<div id="form-submit">
<input type="submit" class="small button radius" value="Submit">
</div>
<!-- mymodal div revealed on form success -->
<script type="text/javascript">
if ($("#contactform").valid()) {
$("#myModal").reveal();
}
</script>
</form>
处理代码(process_form.php)为:
<? ob_start(); ?>
<?php
$to = "destemail@domain.com \r\n";//<== update the email address
$headers = "From: $email_from \r\n";
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = 'an@email.com';//<== update the email address
$email_subject = "Contact via website";
$email_body = "You have received a new message from $name.\n\n".
"Here is the message:\n $message\n\n\n".
$headers .= "Reply-To: $visitor_email \r\n"; //Send the email!
mail($to,$email_subject,$email_body,$headers);
// 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;
}
}
?>
<? ob_flush(); ?>
div '#mymodal' 出现了,但随后页面重定向到 php 文件......我怎样才能阻止它这样做?