Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP form - on submit stay on same page

I have a PHP form that is located on file contact.html.

The form is processed from file processForm.php.

When a user fills out the form and clicks on submit, processForm.php sends the email and direct the user to - processForm.php with a message on that page "Success! Your message has been sent."

I do not know much about PHP, but I know that the action that is calling for this is:

// Die with a success message die("<span class='success'>Success! Your message has been sent.</span>"); 

How can I keep the message inside the form div without redirecting to the processForm.php page?

I can post the entire processForm.php if needed, but it is long.

like image 381
mewebs Avatar asked Jun 27 '13 03:06

mewebs


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 do you stay in the same page after submit a form in HTML?

You could include a hidden iframe on your page and set the target attribute of your form to point to that iframe.

How do I display the HTML form output on the same page?

If you want to add content to a page you need to work with the DOM. Google "create div javascript" or "create span javascript" for examples, you basically need to create an element that has your text in it and add that element to the part of the page you want the text to display.

How can I submit a form without reloading the page?

Use jQuery's submit event to handle the form submit, add return false; at the end of the submit handle function to prevent the page to reload.


1 Answers

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 <?php echo $message; ?>.

Like this:

<?php $message = ""; if(isset($_POST['SubmitButton'])){ //check if form was submitted   $input = $_POST['inputText']; //get input text   $message = "Success! You entered: ".$input; }     ?>  <html> <body>     <form action="" method="post"> <?php echo $message; ?>   <input type="text" name="inputText"/>   <input type="submit" name="SubmitButton"/> </form>     </body> </html> 
like image 172
Tom Groot Avatar answered Sep 28 '22 15:09

Tom Groot