在PHP Conatct表单中重新提交错误

时间:2017-07-28 10:32:04

标签: javascript php jquery forms

为我糟糕的英语道歉。在填写内容并提交时,在我的PHP联系表单中,echo message正在显示。

我尝试刷新“Confirm Form Resubmission”时会显示消息"The mail has been sent successfully"

我需要代码,以便在刷新页面后提交联系表单时,不会显示echo message以及重新提交确认消息。

我刚刚开始使用PHP,我不熟悉它。请有人帮帮我。

PHP脚本

<?php

session_start();

if(isset($_POST['submit']) && $_POST['randcheck']==$_SESSION['rand'])
{

$your_name = $_REQUEST['your_name'];

$email = $_REQUEST['email'];

$mobile_number = $_REQUEST['mobile_number'];

$message = $_REQUEST['message'];

$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";

$to = "mail@hotmail.com";

$subject = "Contact Form";

$mailheader = "From: $email \r\n";


    if (($your_name=="")||($email=="")||($message==""))
        {
        $msg = "All fields are required";
        }
    else{       

       mail($to,$subject,$formcontent,$mailheader);

        $msg = "The mail has been sent successfully";
        }
    } 


?>

HTML脚本

<?php
   $rand=rand();
   $_SESSION['rand']=$rand;
?>

<input type="hidden" value="<?php echo $rand; ?>" name="randcheck" />

<form name="contactform" method="post" enctype="multipart/form-data">

      <label for="your_name">Your Name <font color="red">*</font></label>
      <input  type="text" name="your_name"  placeholder="Enter Your Name" maxlength="20" size="40" value="">

      <label for="email">Email Address <font color="red">*</font></label>
      <input  type="email" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" required value="">

      <label for="mobile_number">Mobile Number</label>
      <input  type="tel" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" value="">

      <label for="message">Message <font color="red">*</font></label>
      <textarea  name="message" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>

        <input type="submit" name="submit" value="Submit">

      <div class="mail"> <?php echo $msg; ?> </div>
        </form>

3 个答案:

答案 0 :(得分:0)

您应该在提交后重定向,如果有人刷新页面,他们会重定向:

header('Location:  / new.php');
exit;

答案 1 :(得分:0)

<强>更新 你去(PHP文件):

<?php
    session_start();
    if(isset($_POST['submit']) && $_POST['randcheck']==$_SESSION['rand'])
    {
    $your_name = $_REQUEST['your_name'];
    $email = $_REQUEST['email'];
    $mobile_number = $_REQUEST['mobile_number'];
    $message = $_REQUEST['message'];
    $formcontent="From: $your_name \n Email: $email \n Phone Number: 
    $mobile_number \n Message: $message";
    $to = "mail@hotmail.com";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
        if (($your_name=="")||($email=="")||($message==""))
            {
                $msg = "All fields are required";
            }
        else{       
                mail($to,$subject,$formcontent,$mailheader);
                $msg = "The mail has been sent successfully";
            }
    } 

unset($_POST['submit']);
?>

HTML:

<?php
   $rand=rand();
   $_SESSION['rand']=$rand;
?>
<form name="contactform" method="post" enctype="multipart/form-data">
      <input type="hidden" value="<?php echo $rand; ?>" name="randcheck" />

      <label for="your_name">Your Name <font color="red">*</font></label>
      <input  type="text" name="your_name"  placeholder="Enter Your Name" maxlength="20" size="40" value="">

      <label for="email">Email Address <font color="red">*</font></label>
      <input  type="email" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" required value="">

      <label for="mobile_number">Mobile Number</label>
      <input  type="tel" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" value="">

      <label for="message">Message <font color="red">*</font></label>
      <textarea  name="message" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>

      <input type="submit" name="submit" value="Submit">

      <div class="mail"> <?php echo $msg; unset($msg);?> </div>
</form>

<强> ____________________________________________________________________________

在if..else块

之后的PHP代码底部添加
unset($_POST['submit']);

echo $msg之后,添加此

unset($msg);

问题在于,当您单击提交按钮时,变量将被存储,每当刷新页面时,初始检查if(isset($_POST['submit']))将返回true,因此您将获得$ msg。

因此,只需在使用后取消设置值即可。

修改

以下是重定向问题的解决方案。 在<form>

的开头添加此内容
<?php
   $rand=rand();
   $_SESSION['rand']=$rand;
?>
  <input type="hidden" value="<?php echo $rand; ?>" name="randcheck" />

然后改变你的

if(isset($_POST['submit']))

为:

if(isset($_POST['submit']) && $_POST['randcheck']==$_SESSION['rand'])

答案 2 :(得分:-1)

使用会话变量尝试这样的事情。

session_start();

if(isset($_POST['submit']))
{

$your_name = $_REQUEST['your_name'];

$email = $_REQUEST['email'];

$mobile_number = $_REQUEST['mobile_number'];

$message = $_REQUEST['message'];

$formcontent="From: $your_name \n Email: $email \n Phone Number: 
$mobile_number \n Message: $message";

$to = "myname@hotmail.com";

$subject = "Contact Form";

$mailheader = "From: $email \r\n";


if (($your_name=="")||($email=="")||($message==""))
    {
    $_SESSION['$msg'] = "All fields are required";
    }
else{       

   mail($to,$subject,$formcontent,$mailheader);

    $_SESSION['$msg'] = "The mail has been sent successfully";
    }
}
else
{ 
   unset($_SESSION['$msg']);

}

HTML部分

<form name="contactform" method="post" enctype="multipart/form-data">

  <label for="your_name">Your Name <font color="red">*</font></label>
  <input  type="text" name="your_name"  placeholder="Enter Your Name" maxlength="20" size="40" value="">

  <label for="email">Email Address <font color="red">*</font></label>
  <input  type="email" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" required value="">

  <label for="mobile_number">Mobile Number</label>
  <input  type="tel" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" value="">

  <label for="message">Message <font color="red">*</font></label>
  <textarea  name="message" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>

    <input type="submit" name="submit" value="Submit">

  <div class="mail"> <?php if(isset($_SESSION['$msg'])){echo $_SESSION['$msg'] }; ?> </div>
    </form>