我有一个PHP表单,每次页面打开/刷新时提交。我只希望它在按下提交按钮时提交。我对PHP完全陌生,所以非常感谢任何帮助。PHP表单每次页面打开/刷新时提交
另外,如果可能的话,我宁愿页面按下提交按钮时不刷新。我想只在按钮下方显示一条消息,表示感谢您提交。我已经搜索了这个主题,但由于含义太模糊,无法理解其他答案。
这里是form.php的:
<?php
$field_name = $_POST['cf_name'];
$field_phone = $_POST['cf_phone'];
$field_email = $_POST['cf_email'];
$field_postcode = $_POST['cf_postcode'];
$field_message = $_POST['cf_message'];
$mail_to = '[email protected]';
$subject = 'Call back form submission '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'Phone: '.$field_phone."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Postcode: '.$field_postcode."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
\t \t print('Thanks, you will receive a call back within the next hour.');
\t \t window.location = 'http://www.domain.com';
\t </script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
\t \t alert('Message failed. Please, send an email to [email protected] to recieve a call back');
\t \t window.location = 'http://www.domain.com';
\t </script>
<?php
}
?>
这里是HTML:
<form class="form-horizontal" action="bat/form.php" method='post'>
<div id="container">
<div id="form1"><div class="form-group">
<div class="col-sm-10 input-group-lg">
<input type="text" class="form-control" placeholder="E-mail" name='cf_email'>
</input>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 input-group-lg">
<input type="text" class="form-control" placeholder="Postcode" name='cf_postcode'>
</input>
</div>
</div>
</div>
<div id="form2"><div class="form-group">
<div class="col-sm-10 input-group-lg">
<input type="text" class="form-control" placeholder="Name" name='cf_name'>
</input>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 input-group-lg">
<input type="text" class="form-control" placeholder="Phone number" name='cf_phone'>
</input>
</div>
</div></div>
</div>
<div class="form-group">
<div class="col-sm-10 input-group-lg">
<input type="text" class="form-control" placeholder="Message" name='cf_message'>
</input>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="submit" class="btn btn-primary btn-med" value='Submit »'>
</input>
</div>
</div>
</form>
其实发生了什么事,一次提交后,当你按下agai f5然后重新提交?是这样吗? –
有很多关于如何解决这个问题的指南,你不是第一个在表单重新提交时遇到问题的人,因为你没有清除头部的数据,至于其他部分:使用Ajax 。 – Epodax