Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return to the previous page after a submit form with php?

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 = "[email protected]";
  $message = '
  name: '.$name.'
  email: '.$email.'
   message: '.$message.'
  ';

 $headers = 'From: [email protected]';

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

 ?>
like image 399
industriales-blue Avatar asked Mar 30 '14 14:03

industriales-blue


People also ask

How do I stay on the same page after submitting form in PHP?

In order to stay on the same page on submit you can leave action empty ( action="" ) into the form tag, or leave it out altogether. For the message, create a variable ( $message = "Success! You entered: ". $input;" ) and then echo the variable at the place in the page where you want the message to appear with <?

How can I join another page in PHP?

How Redirection Works in PHP. In PHP, when you want to redirect a user from one page to another page, you need to use the header() function. The header function allows you to send a raw HTTP location header, which performs the actual redirection as we discussed in the previous section.

How do I submit a form to another page?

To link a submit button to another webpage using HTML Form Tags: Using the HTML Form's Action Attribute, we can link the submit button to a separate page in the HTML Form element. Here, declare/write the “Submit button” within HTML Form Tags and give the File Path inside the HTML form's action property.

How can I redirect one PHP file to another PHP file?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.


2 Answers

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.

like image 105
Nick Beeuwsaert Avatar answered Sep 23 '22 19:09

Nick Beeuwsaert


Add a JS redirect after your alert then

echo "<script>
             alert('message sent succesfully'); 
             window.history.go(-1);
     </script>";
like image 29
Hanky Panky Avatar answered Sep 21 '22 19:09

Hanky Panky