Hello all,

I have a form done in php that posts data to my database, and it is working perfectly.

I currently have it display the words "Thank you, form submitted" after it is submitted, but what I would like to do is automatically take the viewer to a specified URL after the form is submitted.

Is there an easy way to do this? I am a complete php novice, and if someone could help me out I would appreciate it.

Thank you very much,
Sarah

    yes there is - search this forum and you will find that this question has been answered several times

      You can use the header function:

      header("Location:http://www.yoursite.com/desired_page.php");

      in the form processor file immediately once you have put the form data into the table.

      n.b. You can't put any HTML/output before this, and the jump will happen once this line is reached, so this file won't get 'seen' by the surfer. ( make sure there isn't any space/blank lines in your editor before the opening <? )

      You could put in a query string:

      header("Location:http://www.yoursite.com/desired_page.php?success=1");

      and on the destination page, put on top:

      <?
      if ($success==1) {
      echo("<B>Thank You, form submitted.</B>");
      }
      ?>
      ...rest of page

      hope this helps

        Like "that guy" kinda stated: use the search function, or better: Read The Manual...

          Thanks for the replies...

          that guy: I have checked the archives, but all of the solutions presented don't seem to work for me, I keep getting syntax errors (see code snip below.)

          chakotha: When I try the first method you suggest, I get a "headers already sent" error.

          Here is the first bit of code I tried:


          $result = mysql_query($sql);

          print mysql_error();

          print "<br><br>";

          print "Thank you! Information entered.\n";

          print "<META HTTP-EQUIV=Refresh CONTENT="10; URL=http://www.htmlhelp.com/">";

          } else{


          All this does is give me a syntax error...

          and if I try this:


          $result = mysql_query($sql);

          print mysql_error();

          header("Location:http://www.yoursite.com/desired_page.php");

          } else{


          I get a "headers already sent" error

          I apologze if I seem like a real moron with this stuff, but I really am trying to learn. Thank you for the help, I appreciate it.

          Thanks again,
          Sarah

            Okay, check out the PHP manual (at www.php.net) and you will see that you need to send headers BEFORE you send any output to the browser. So, you need to put the header somewhere at the top...

              If I do that, won't it automatically just forward before people submit the page?

              Or does the header only get called once the form is submitted?

              I'm really sorry if I am making some of you angry- I am not a programmer, I'm just trying to teach myself. The manual at php.net starts out simple, but then turns into something incomprehensible to someone who doesn't know how to program...

                just for the beginners... PHP is actually quite simple just keep in mind, that anything done by PHP is done on the webserver, never on the clientside.

                Therefore you "visitor" request your form page from the webserver (e.g. form.php), the webserver executes all the php stuff and sends the result as a "normal" HTML document to your visitor. As long as the "visitor" does nothing, no php is executed. After the "visitor" submits the form, the specified page is requested (e.g. save_formdata.php) all the PHP in there gets executed (e.g. validate the form data, save the form data in a db etc.) and if you place the Header() call at the end of your php script, the specified page (thankyou.htm) gets called, without the user noticing that there was a page in between.

                Hope that gave you a quick overview over the really basic meaning of Server Side ...

                On the other hand: If you want to display a message: "Thanks for submitting" and want to change the page after displaying this message for e.g. 5 sec. You need JavaScript, remember a Webserver is only able to ANSWER Request, a Webserver can't send a document that wasn't requested by the client (Therefore PHP is pretty useless for this task). Therefore you need to emulate the client request via Java Script, the Metatag you mentioned pointed in the right direction... just some syntax errors, try:
                echo "<META http-equiv=\"refresh\" content=\"5; URL=http://www.php.net\">\n";

                hope that helped
                and if you looked around and can't find an answer just ask, IMHO that's what a forum is designed for 🙂
                Rob

                  Thanks for the great reply rob.

                  I actually solved my problem by doing this:


                  $result = mysql_query($sql) or die(mysql_error());?>

                  <META HTTP-EQUIV=Refresh CONTENT="0; URL=http://www.mysite.com/thanks.php">

                  <?

                  } else{


                  It's not pretty, but it seems to be working.

                  I also got a suggestion to split the page into two, and have all of the form data sent on the thank-you page, which I may try just for fun...

                  Thnaks again!

                    This is a big, and very common, problem. The PHP manual is targetted at people who already know how to program; that's why it so quickly gets into territory that is incomprehensible to you. There are lots and lots of books on PHP out there, some of them aimed at the beginner, but none that I would recommend very much. At the risk, perhaps, of getting flamed for mentioning something non-PHP here, you might find something like the python tutorial a better introduction to programming per se.

                    An important part of the problem for beginners is the webserver environment, and this is of course not specific to PHP. Instead of a nice development environment complete with a visual debugger you get to embed print statements to try to see what is going wrong.:-(

                      Thats right!
                      I allready had a lot of programming (COBOL g) experience as I started with PHP and found it quite easy to learn. Well and I allready had a lot of experience with "help files using" so i knew how and where to look for solutions to my problems.

                      But if you have no programming experience at all, i guess your right Kirk, you should try to get yourself some information on how to programm (remember: If you know one programming language, you know all).

                      To the books: If you (sarah) want to buy yourself a book about PHP, have a really close look at it, because i found a lot of Books that are nothing more than a reformated (and translated <- depending on your languange) version of the PHP Manual, and i think 50$ are a bit to much for the paper...

                      cya Rob

                        Oh, sorry 'bout that misunderstanding: you're not making me angry, but to me it seemed quite easy to look at the manual and find out how a function works. If I get errors, I look in the manual to see what I might be doing wrong with that function... That was my point. 🙂

                          Write a Reply...