2

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" >
Jack
  • 8,851
  • 3
  • 21
  • 26
user1191294
  • 23
  • 2
  • 5
  • Nit picky point: Your form does not "send data to mysql". It gets sent to a PHP script, and THAT script deals with mysql. – Marc B Feb 06 '12 at 14:53

4 Answers4

3

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.

Grim...
  • 16,518
  • 7
  • 45
  • 61
  • I really think this is overkill (or maybe I misunderstood the question...) – JMax Feb 06 '12 at 14:45
  • 1
    The `header` should be used after the `mysql` operation and before any output. Assuming the current file handles the mysql operation. – gustyaquino Feb 06 '12 at 14:47
  • @JMax: Really? As I said, I like to keep the insert code on the same page as the form itself is called, so I often use a header redirect (if I'm not using templates). I guess there's a tiny bit more overhead on the server because it's loading two files, but I'm fairly sure the OP is not implementing it on a high-traffic site ;-) – Grim... Feb 06 '12 at 14:47
  • hmm it does not work. basically i need the submit button to send the infor to mysql (which i have done and is working) but after that go to thankyou.html – user1191294 Feb 06 '12 at 14:57
  • Putting the code above after your MySQL insert code should work fine. Are you getting an error? – Grim... Feb 06 '12 at 14:58
  • when i put it straight above the mysql insert form. It loads everything but the form :s – user1191294 Feb 06 '12 at 15:02
  • @user1191294: you should consider using a full HTML solution to solve your issue as I've suggested in my answer. (EDIT) David's answer is more complete. I will delete mine for his. – JMax Feb 06 '12 at 15:02
  • @Grim...: I understand this is the way you handle it (and this is probably one of the best way when using templates). Yet, it isn't the simpliest way to answer the OP's question and IMHO, it isn't the easiest pure HTML way. – JMax Feb 06 '12 at 15:04
  • @user1191294 The header code should go inside the same `if` statement that your SQL inserting code uses. – Grim... Feb 06 '12 at 15:05
  • @JMax - the main problem with your way (as presented) is that nothing will be inserted into the database. – Grim... Feb 06 '12 at 15:06
  • @Grim...: I get your point. I've assumed that `thankyou.html` contained the SQL insertion PHP code :) – JMax Feb 06 '12 at 15:09
1

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)

David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    "This would essentially be the last thing the PHP code does after processing your form." That's not the case. The header only effects the browser - the PHP script will continue to run until it terminates, so you should always follow that line with `exit;`. It's something you normally learn the hard way =] – Grim... Feb 06 '12 at 15:00
  • thanks David and Grim, @Grim you say the put the header code should go inside the same if statement that your SQL inserting code uses. How do you mean? could you fire up an example if poss? – user1191294 Feb 06 '12 at 15:30
  • My answer has a rough example in - basically you should only be submitting data to the database if there is something to submit - and you should only redirect if that submission is successful. – Grim... Feb 06 '12 at 15:37
  • @user1191294: How does your PHP code currently discern if it's initially showing the page or if it's responding to the form action? That's the conditional in question. The usual pattern (in pseudo-code) is: `if (handling_a_form_post) { do DB_insert; do redirect; } else { do display_page; }` – David Feb 06 '12 at 15:37
  • can you look at fleppars post directly below this. It seems to be working but is putting %20?>?submit=1 after the link the form is on? – user1191294 Feb 06 '12 at 16:42
  • @user1191294: That looks like a very odd workaround, I wouldn't recommend it. The server code can determine if it's a POST or a GET by the request type (see this question: http://stackoverflow.com/q/359047/328193) or by looking for specific values in the POST (such as the `submit` value in the code you've shown so far), which it should do for input checking anyway. Again, it depends on how your PHP code currently discerns a form submission from a page load. You can edit your question to include code to demonstrate. – David Feb 06 '12 at 16:48
  • @user1191294: The `if` statement in his example shows the logic flow of what we're talking about, but his method for determining if something is a POST is somewhat hack-ish. – David Feb 06 '12 at 16:49
  • im pretty confused to be honest. Im kind of new to PHP. all help is appreciate. I have uploaded what I have tried can you take a look at my code here: http://jsfiddle.net/27TV5/ you can edit if necessary thanks – user1191294 Feb 06 '12 at 17:25
  • @user1191294: The code looks pretty accurate to the situation being described. The `if ($POST)` conditional is what checks if the current request is a form being submitted. So, as state in the code comment, that's where you'd handle your form input. (Do input checking, insert/update the database record(s), etc.) Then the last thing to do within that `if` statement is the redirect and the `exit`, which you have in that code. – David Feb 06 '12 at 17:51
0

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/'); ?>
Fleppar
  • 84
  • 6
  • thanks this just puts %20?>?submit=1 after my link? e.g. say my form was on www.google.com/login.php it goes to www.google.com/login.php%20?>?submit=1 when the submit button is clicked – user1191294 Feb 06 '12 at 15:27
0

could always use JS

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


    //do mysql stuff here

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