我有一個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