user1191294
user1191294

Reputation: 23

how to have 2 actions on 1 submit button

I have a form with a submit button. When the submit button is clicked it sends the information to mysql. This is all working however it when submit button is clicked it just reloads the page. I want it to to go to send the info to mysql then go to page thankyou.html.

my submit button is:

<input type="submit" name="submit" id="submit" value="Submit" /> 

my form action is:

<form name="list"; action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >

Upvotes: 2

Views: 3429

Answers (4)

Grim...
Grim...

Reputation: 16953

This php code will fire you off to another page:

<?php
if ($_POST) { // check something is being sent by the form
    // MySQL stuff here...
    header("location:thankyou.html");
    exit;
}
?>
// put the form here

Or you can put the php that inserts data to the database at the top of thankyou.html and have the form submit there instead.

Personally I like to keep the insert on the same page as the form, but it's just personal choice.

Important: There can't be any output to the page before the php redirect is called, not even whitespace, so make sure to put your opening php tags right at the top of the file and not to print or echo anything.

Upvotes: 3

David
David

Reputation: 218818

Since you're submitting the form to the same page, the server will (after processing any PHP code on that page), display that page. You have a couple of options here:

  1. Submit the form to a different page.
  2. Redirect from the page after processing the form.

The first option would mean that you'd change the action in the form to something like this:

<form name="list" action="formhandler.php" method="post">

This would mean that formhandler.php (or whatever you decide to call it) would have the server-side PHP code to process the form, and then that page would display whatever you want it to display.

The second option would involve just using a redirect in you PHP code (on the page to which you're already submitting) to direct the user to a different page after processing the form. The PHP header() function is the standard way to do this. Something like this:

header("Location: thankyou.php");

This should essentially be the last thing the PHP code does after processing your form. That is, you probably don't want your server-side code to continue to execute after what would intuitively be a terminating case. So you'll want to call exit immediately after it to stop page execution. (Keep in mind that the actual redirect isn't a server-side action, the server is just sending the Location header to the browser and telling the browser that it should perform the redirect.)

(Also, as noted in other answers here, there should be otherwise no output to the page when performing such a redirect.)

Additionally, I suppose you could also have conditional output on your PHP page, displaying one set of HTML content (the form) if it's not handling a POST or another set of content (the thank you page) if it is handling a POST, but that feels like a slippery slope to me. It's better to separate your server-side resources than to have one named resource (somepage.php) that does different things conditionally. (Single Responsibility Principle)

Upvotes: 1

Fleppar
Fleppar

Reputation: 84

I would suggest just putting the action to the thankyou.php page and placing the mysql insertion code on that page using post for the data.

<form name="list" action="thankyou.php" method="post">

Additionally if you want everything on the same page, you could use

<form name="list" action="<?php echo $_SERVER['PHP_SELF']; ?>?submit=1" method="post" >

And in the PHP code at the top of the page, do a quick

<?php if ($_GET['submit'] == '1') header('Location: http://www.example.com/'); ?>

Upvotes: 0

user849137
user849137

Reputation:

could always use JS

<? if(!empty($_POST['submit']){


    //do mysql stuff here

    ?>
    <script type="text/javascript">
    <!--
    window.location = "thankyou.html "
    //-->
    </script><?
    }?>

Upvotes: 0

Related Questions