2016-09-19 74 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