7

I have a form in html whose "action" attribute calls a contact.php file. The problem is that after I submit the form, the file that is visible is a blank page with the address contact.php and I want to see again the form of the main page.

HTML:
<form id="myForm" action="php-contact/contact.php"
                       method="post" class="contact_form" autocomplete="off"
                       role="form">
                          <div class="form-group">
                            <label for="input-subject">subject</label>
                            <input name="subject" type="text" class="form-control" id="subject" placeholder="your subject" maxlength="20"/>
                          </div>

                          <div class="form-group">    
                              <label for="input-name">name</label>
                              <input name="name"  type="text" class="form-control" id="name" placeholder="your name" maxlength="20"/>
                          </div>

                          <div class="form-group">    
                            <label for="input-email">email address</label>
                            <input name="email"  type="text" class="form-control" id="email" placeholder="your email" maxlength="40"/>
                          </div> 

                          <div class="form-group" >   
                              <label for="input-message">message</label>
                              <textarea name="message" cols="10" rows="10"  class="form-control" id="comment" ></textarea>
                          </div>  

                          <button name="myFormSubmitted" type="submit" class="btn btn-primary btn-lg btn-block">send</button>
                      </form>

PHP:
  <?php
  $name = $_POST['name'];
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $message = $_POST['message'];

  $to = "pep.g@gmail.com";
  $message = '
  name: '.$name.'
  email: '.$email.'
   message: '.$message.'
  ';

 $headers = 'From: pep.website@website.com';

   if (
    mail($to, $subject, $message, $headers)
) 
      echo"<script>alert('message send succesfully')</script>";
   else
      echo"<script>alert('message not send')</script>";

 ?>
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95

5 Answers5

13

Use either $_SERVER["HTTP_REFERER"] or use a hidden field on the form with the url of the current page:

<form action="myAction.php">
    <input type="hidden" name="destination" value="<?php echo $_SERVER["REQUEST_URI"]; ?>"/>
    <!-- other form inputs -->
    <input type="submit"/>
</form>

myAction.php

<?php
  /* Do work */
  if(isset($_REQUEST["destination"])){
      header("Location: {$_REQUEST["destination"]}");
  }else if(isset($_SERVER["HTTP_REFERER"])){
      header("Location: {$_SERVER["HTTP_REFERER"]}");
  }else{
       /* some fallback, maybe redirect to index.php */
  }

And then even then you should have fallbacks in case for some reason the client isn't respecting HTTP redirects, like some redirect javascript, a meta redirect and a link to the destination.

Alexander Ushakov
  • 5,139
  • 3
  • 27
  • 50
Nick Beeuwsaert
  • 1,598
  • 1
  • 11
  • 18
  • This is a better and more reliable way to do it when submitting data to the database or when variables are attached to a url. You don't want the browser to just go back a page (which could potentially end up submitting a form multiple times or displaying errors), you want it to physically go to the url, effectively reloading the page with the new data, with the option to attach additional variables to the url (like a success/error message). – PBwebD Apr 15 '15 at 17:58
8

Add a JS redirect after your alert then

echo "<script>
             alert('message sent succesfully'); 
             window.history.go(-1);
     </script>";
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • I tried with your solution and the page return this: alert('message sent succesfully'); window.history.go(-1); "; else echo""; ?> – industriales-blue Mar 30 '14 at 15:08
  • Then your PHP code is breaking somewhere. look for proper open and close tags – Hanky Panky Mar 30 '14 at 15:13
  • It´s work. I put the script outside of php code....But the alert appears on a blank page and only after the form is loaded. I want to appear the alert (if the form is properly sent) by the form load – industriales-blue Mar 30 '14 at 16:30
0

In your contact.php file, just use something like at the end of the PHP code:

header("location:yourfilenamehere.php");    

This will redirect back to whatever page you specify.

Charlie74
  • 2,883
  • 15
  • 23
0

You could do two things...

1) Have your php logic in the form file and submit to that file. On the top of the file, put something like:

if(isset($_POST['name'])) {
// your sending logic
}

followed by your form.

2) use the header() function to redirect to your form.

header("Location: form.html"); 
Maarten van Middelaar
  • 1,691
  • 10
  • 15
  • This wont work here since they are already sending output in the form of a JavaScript alert – Hanky Panky Mar 30 '14 at 15:00
  • @Hanky웃Panky that's true. Wouldn't be my first choice though. Perhaps header("Location: form.php?send=success"); and check for $_GET['send'] to display a succes message above the form – Maarten van Middelaar Mar 30 '14 at 15:04
0

I feel pretty bad about posting this answer as it is just concatenating two other SO answers.

First part

Charlie74's answer on this page

// add the message you want to show to the user
header("Location: yourfilenamehere.php?alertmsg=message+send+succesfully"); 
exit();

Second part check for the alertmsg name in the query string

See SO post for getParameterByName:

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

in the page you are redirecting call the getParameterByName function:

<script>
var msg = getParameterByName('alertmsg');

if (alertmsg.length > 0) {
    alert(msg);
}
</script>
Community
  • 1
  • 1
robbmj
  • 16,085
  • 8
  • 38
  • 63