Jump to content

Submit form but stay on same page?


ffxpwns

Recommended Posts

I have the website here: http://bossexclusive.com/babyauction/index.php , and http://bossexclusive.com/babyauction/thanks.php .  When you press submit on the index.php, it takes you to thanks.php. then, after 5s, it takes you back to index.php.  My question is: How could I make it so when I  press submit, the box submits the form, but instead of redirecting to thanks.php, it displays the same thank you message on the side, but stays on index.php? If that didn't make sense, let me know. My html and php are below

(HTML)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Baby Auction | Discounted Premium Brand Baby Products</title>
<link rel="stylesheet" type="text/css" href="styles.css"/>
</head>
<body>
<div id="main">
  <div id="contact">
  <h1>Sign up to recieve Auction notifications</h1>
    <form action="emailer.php" method="post">
     <input type="text" name="email" id="email" placeholder="yourname@email.com" />
    <input type="image" name="submit" src="images/submit.jpg" width="146px" height="20px" value="Submit">
    </form>

  </div>
</div>
<div class="copyright">
<?php
include 'footer.php';
?>
</div>
</body>
</html>

 

and emailer.php:

 

<?php

$email = trim(strip_tags($_POST['email']));


       
       $subject = "Contact form submitted!";
       $to = 'BlankingOutMyEmail@gmail.com';

       $body = <<<HTML
$message
HTML;

       $headers = "From: $email\r\n";
       $headers .= "Content-type: text/html\r\n";

       
       mail($to, $subject, $body, $headers);
      
      header('Location: thanks.php');
?>

Link to comment
Share on other sites

<form action="emailer.php" method="post">

 

That seems to recirect you to another page. Remove that and you'll post to the same page you are on.

Sure, I can put it on the same page and remove the header('Location: thanks.php');, but how can I put a thankyou note like the one on thanks.php when submit is pressed without leaving the page?

Link to comment
Share on other sites

what you can do is make it submit to itself by leaving action blank, inserting your php code at the top of the page where the form is...make sure you add an if() to make sure that it executes when the form is submitted...then under your mail() just echo your thankyou message and then stop the browser with die() after you've echoed out the message

Link to comment
Share on other sites

what you can do is make it submit to itself by leaving action blank, inserting your php code at the top of the page where the form is...make sure you add an if() to make sure that it executes when the form is submitted...then under your mail() just echo your thankyou message and then stop the browser with die() after you've echoed out the message

OR, instead of die; just set a variable and within body tag add so the rest of the page loads as it should.  Never the less, putting processing above html is a good idea.

<?php if (isset($message)){ echo $message; } ?>

You can even tie this message into the form so it hides the form.

<?php 
if (isset($message)){ 
echo $message; 
}else{
//form goes here
} 
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.