-1

actually facing some problems with my php contact form. problem Number 1: After submit the contact form if the user reload the browser they get an message from browser alert

Confirm Form Resubmission

The page that you're looking for used information that you entered. Returning to that page might cause any action you took to be repeated. Do you want to continue?

Continue button and Cancel button

Problem Number 2: after submit the form i'm getting mails on my mailbox but i'm getting it via hostinguser@webserver.com so is that any problem?

here is my all code please make me currect if i'm doing wrong or something like that. thanks

<head>
<title>Form submission</title>
</head>
<body>

<form method="post">
First Name: <input type="text" name="client_name"><br>
Last Name: <input type="text" name="client_phone"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>

<?php 
if(isset($_POST['submit'])){
    $to = "mailmenow23@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $client_name = $_POST['client_name'];
    $client_phone = $_POST['client_phone'];
    $subject = "Form Submission by"." " $client_name ;
    $message = "Client Name : ". $client_name ."\n\n". "Client Phone : " . $client_phone ."\n\n"." wrote the following:" . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
    echo "Mail Sent. Thank you " . $client_name . ", we will contact you shortly.";

    }
?>

</body>
</html>```
udaysarkarud
  • 1
  • 1
  • 2
  • 1
    About issue 1: let view in here https://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-f5-ctrlr – Au Nguyen Nov 05 '19 at 04:07

2 Answers2

0

Just redirect the user back to the form page once the form gets submitted to the server. Use either session or cookie to store success/failure message so you can output it on your form page.

<?php session_start(); ?>
<head>
<title>Form submission</title>
</head>
<body>

<?php 
if( isset( $_SESSSION['status' ] ) ){
   echo '<p>'. $_SESSSION['status' ] .'</p>';
   unset( $_SESSION['status'] ); // unsetting the status index
}
?>  

<form method="post">
First Name: <input type="text" name="client_name"><br>
Last Name: <input type="text" name="client_phone"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>

<?php 
if(isset($_POST['submit'])){
    $to = "mailmenow23@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $client_name = $_POST['client_name'];
    $client_phone = $_POST['client_phone'];
    $subject = "Form Submission by"." " $client_name ;
    $message = "Client Name : ". $client_name ."\n\n". "Client Phone : " . $client_phone ."\n\n"." wrote the following:" . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);


     // storing the message in the session
     $_SESSION['status'] = "Mail Sent. Thank you " . $client_name . ", we will contact you shortly.";

    // redirecting the user to the form page
    header('Location: form-page-name.php');


    }
?>

</body>
</html>

Furthermore, do not forget to sanitize and validate the user inputs upon form submission.

Assad Ullah Ch
  • 706
  • 7
  • 12
0

This is solved with the Post-Redirect-Get pattern. Any time you get data through a post, you redirect after working with the data.

This also leads to a programming convention that is very helpful when using procedural style scripting. Following this convention will help you avoid technical debt and spaghetti code:

  1. Perform initialization and load any configuration files
  2. Work with any user input. Redirect back to same page or next in series.
  3. Perform database calls and or business logic.
  4. Never output anything until after all your php stuff is done. Separate logic and presentation as much as possible. Only at this point are you ready to output html. Use php only for looping and variable substitution at this point

Using these principles, your script would look something like this:

<?php 
session_start();

if(isset($_POST['submit'])){
    $to = "mailmenow23@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $client_name = $_POST['client_name'];
    $client_phone = $_POST['client_phone'];
    $subject = "Form Submission by"." " $client_name ;
    $message = "Client Name : ". $client_name ."\n\n". "Client Phone : " . $client_phone ."\n\n"." wrote the following:" . "\n\n" . $_POST['message'];


    // warning! This is open to injection! 
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);

    $_SESSION['message'] = "Mail Sent. Thank you " . $client_name . ", we will contact you shortly.";

    // cannot have any output before this!
    // see https://www.php.net/manual/en/function.header.php

    header('Location: ' . htmlentities($_SERVER['PHP_SELF '));
    exit; // prevent script from continuing

}
// do any other logic stuff, I.e., database calls, calculations, formatting functions, etc.

// now that all php stuff is done, it’s time to present the view
?>
<html>
    <head>
      <title>Form submission</title>
    </head>
    <body>
        <?php if(array_key_exists('blah',$_SESSION) && !empty($_SESSION['blah'])):?>
        <div class= "alert-success"><?= $_SESSION["message"] ?></div>
        <?php endforeach;?>
        <form method="post">
            First Name: <input type="text" name="client_name"><br>
            Last Name: <input type="text" name="client_phone"><br>
            Email: <input type="text" name="email"><br>
            Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
            <input type="submit" name="submit" value="Submit">
        </form>
    </body>
</html>
Tim Morton
  • 2,614
  • 1
  • 15
  • 23