我有一个简单的表单,我使用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文件...我怎么能阻止它这样做?
答案 0 :(得分:0)
你似乎没有阻止提交,除非我错过了。
您可能希望编写一些javascript来阻止默认操作onsubmit,而是使用AJAX将表单数据发送到您的处理器PHP脚本。 php会返回您要返回的内容,AJAX会在您的模态窗口中显示它。
我首先看一下Javascript的preventDefault和JQUERY库的.ajax函数。