2016-09-19 71 views
-1

我有一個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 &raquo;'> 
 
      </input> 
 
     </div> 
 
     </div> 
 
    </form>

+0

其實發生了什麼事,一次提交後,當你按下agai f5然後重新提交?是這樣嗎? –

+1

有很多關於如何解決這個問題的指南,你不是第一個在表單重新提交時遇到問題的人,因爲你沒有清除頭部的數據,至於其他部分:使用Ajax 。 – Epodax

回答

0

你應該使用Java腳本重定向頁面,它阻止你提交表單時刷新頁面

echo "<script>window.open('samepage.php' , '_self')</script>"; 

使用此代碼在您的形式要求 的解析結束時,我認爲你的問題得到解決

+0

謝謝!這工作!不幸的是它仍然發送一個空白的提交電子郵件以及提交的電子郵件。你知道爲什麼會發生這種情況嗎? – user2153772

0

<?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 
 
    } 
 
    ?> 
 
\t 
 
\t 
 
<!DOCTYPE html> 
 
       <head> 
 
       <meta charset="utf-8"> 
 
       <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
       <meta name="generator" content="HTML Template"> 
 
\t \t \t \t <title> Test </title> 
 
       </head> 
 
    <body> 
 

 

 
    <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 &raquo;'> 
 
       </input> 
 
      </div> 
 
      </div> 
 
     </form> 
 

 

 

 
</body> 
 
</html>

試試這個

將文件另存爲.php