-1

I have a contact form which sends an email once submitted and displays an alert successfully when sent. My problem was 1) after its sent the email form still has content. 2) If I refresh the page the email is sent again with the previous details. In order to solve this I used the below code in php which is doing the job of refreshing with out sending an email.

header("Location: contact.php");
exit;

Now my problem is The alert is not displayed anymore which is true because the alert is in my HTML and header refreshes the page and it never reaches the alert in HTML(displayed using php echo). Here is the HTML Code :

<div class="container">
  <div class="row">
    <div class="col-md-6 col-md-offset-3" id="emailForm">
      <h1 class="center">Email Form</h1>
      <?php echo $result; //header( "Location: contact.php"); //exit; ?>
      <p>Please get in touch ! I will get back to you as soon as I can !!</p>
      <form method="post" id="contactForm">
        <div class="form-group">
          <label for="name">Your Name</label>
          <input type="text" name="name" class="form-control" placeholder="Your Name" value="<?php echo $_POST['name'] ?>" />
        </div>

        <div class="form-group">
          <label for="email">Your Email</label>
          <input type="email" name="email" class="form-control" placeholder="Your Email" value="<?php echo $_POST['email'] ?>" />
        </div>

        <div class="form-group">
          <label for="message">Message</label>
          <textarea class="form-control" name="message">
            <?php echo $_POST[ 'message'] ?>
          </textarea>
        </div>
        <input type="submit" name="submit" id="send" class="btn btn-success btn-lg center" value="Submit" />
      </form>
    </div>
  </div>
</div>

Here is the PHP code:

<?php

if($_POST['submit']) {

    if(!$_POST['name']){

        $error.="<br>Please enter your name";
    }
    if(!$_POST['email']){

        $error.="<br>Please enter your email address";
    }
    if(!$_POST['message']){

        $error.="<br>Please enter a message";
    }

    if ($_POST['email']!= "" AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {

        $error.="<br>Please enter a valid email address";
    } 

    if( $error )
    {
        $result='<div class="alert alert-danger"><strong>There were error(s):'.$error.'</strong></div>';
    }

    else {

        if(mail("madistackcloud@gmail.com","Message from Deepak", "Name: ".$_POST['name']."Email: ".$_POST['email']."Message: ".$_POST['message'])) {

            $result='<div class="alert alert-success alert-dismissible"><strong>Thank you! I\'ll be in touch.</strong></div>';   


        }

        else
        {
            $result='<div class="alert alert-danger alert-dismissible"><strong>Oops! Something went wrong. Please try again !</strong></div>';
        }

    }
}

?>

Is there any workaround for this or any other method which can do the job. Kindly help.

madistack
  • 1
  • 3
  • Possible duplicate of [How to reset (clear) form through JavaScript?](http://stackoverflow.com/questions/3786694/how-to-reset-clear-form-through-javascript) – Szabolcs Páll Oct 02 '15 at 16:15
  • @SzabolcsPáll I tried that solution even before posting the question. I used $("#client")[0].reset(); right after I echo my result. For some reason the form isn't being displayed at all. Hence posted the question – madistack Oct 02 '15 at 16:19
  • @MrLister Its a typo changed it. Thanks – madistack Oct 04 '15 at 03:22

3 Answers3

0

Change your form to

  <form method="post" id="contactForm">
    <div class="form-group">
      <label for="name">Your Name</label>
      <input type="text" name="name" class="form-control" placeholder="Your Name" value="" />
    </div>

    <div class="form-group">
      <label for="email">Your Email</label>
      <input type="email" name="email" class="form-control" placeholder="Your Email" value="" />
    </div>

    <div class="form-group">
      <label for="message">Message</label>
      <textarea class="form-control" name="message"></textarea>
    </div>
    <input type="submit" name="submit" id="send" class="btn btn-success btn-lg center" value="Submit" />
  </form>

If you don't want to show the content sent, dont use echo $POST on the value of the inputs.

Berriel
  • 12,659
  • 4
  • 43
  • 67
  • Lets say the user made a mistake while entering his name or email it would not be a good thing to reenter all the info all over again. Hence I used echo to pop those values so that user can edit it – madistack Oct 02 '15 at 16:04
  • 'made a mistake' is not predictable. You can check if he missed one of the inputs, then show the imcomplete content sent again, but that would be a different question... – Berriel Oct 02 '15 at 16:12
  • Yes, that is what I wanted to achieve if he missed one of the input then it would be useful. – madistack Oct 02 '15 at 16:15
  • So, you need to read about form validation. Try to implement something, than open a new question if you have a problem that isn't already answered in SO. – Berriel Oct 02 '15 at 16:17
  • I tried and looked for some stuff as well and only then I posted it. Thanks for the advise. I will look more on my own. – madistack Oct 02 '15 at 18:00
  • Just said that this question wasn't about validation, was about "resetting a form" as the title says. Formulate you question again, about the validation. But first, read about it on SO. There are tons of stuff about it already. – Berriel Oct 02 '15 at 19:35
0

You're always echoing the post data in the html section. You only want to echo data if there is an error.

value="<?php echo $_POST['name'] ?>"

in the control code above do something like

$name = cleanInput($_POST['name']);
$email = cleanInput($_POST['name']);
// etc...

Then in your

if( $error )
...
else
{
    $name = "";
    $email = "";

    sendMail();
    // etc...

and then back in your html:

value="<?php echo $name; ?>"
Rob22
  • 81
  • 6
0

Why do you do this: value="<?php echo $_POST['email']? This sets the value of the input field to what the user inputted. This means next time you load your page this value is visible and thus will be emailed when transmitted.

Hiltje
  • 140
  • 1
  • 5
  • Lets say the user made a mistake while entering his name or email it would not be a good thing to reenter all the info all over again. Hence I used echo to pop those values so that user can edit it – madistack Oct 02 '15 at 16:09