简体   繁体   中英

Resubmitting Error in PHP Conatct form

Apologies for my bad English. In my PHP contact form while I fill out the contents and submit, the echo message is showing.

While I try to refresh “Confirm Form Resubmission” is showing along with the message "The mail has been sent successfully" .

I need code for when I submitted the contact form after refresh the page won't display the echo message along with the confirm for re-submission message.

I am just starting out with PHP and I am not familiar with it. Can anybody help me please.

PHP script

<?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 script

<?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>

You should redirect after submitting, if someone refresh the page they go to redirect:

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

UPDATE: Here you go (PHP File):

<?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>

____________________________________________________________________________

Add this at the bottom of PHP code, after the if..else block

unset($_POST['submit']);

And after echo $msg , add this

unset($msg);

The problem is that when you click on the submit button, variable gets stored and whenever the page is refreshed the initial check if(isset($_POST['submit'])) returns true, so you get the $msg.

So, simply unset the values after using them.

EDIT:

Here's the solution for Redirect problem. Add this at the start of your <form>

<?php
   $rand=rand();
   $_SESSION['rand']=$rand;
?>
  <input type="hidden" value="<?php echo $rand; ?>" name="randcheck" />

And then change your

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

to:

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

USE SESSION VARIABLE TRY SOMETHING LIKE THIS.

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 part

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM