cbr_600rr Posted November 16, 2009 Share Posted November 16, 2009 ok, I have a file called test1.php, I run a form that obtains some information from a user. I set <form action="test1.php".........> so that It doesn't redirect the user to a different page yet. I then put the information i obtained from the form into a variable $test1var I load a database and insert it into a table. close mysql connection Now I want it to redirect to test2.php after I insert the variable into the database. I can't seem to figure out a way to get this to go to another php file after I close my database. If i set the form action=test2.php , then it skips the whole mysql thing. any ideas? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/181683-redirect-to-another-php-file-after-closing-mysql/ Share on other sites More sharing options...
wee493 Posted November 16, 2009 Share Posted November 16, 2009 after you close mysql do this header("Location: test2.php"); Quote Link to comment https://forums.phpfreaks.com/topic/181683-redirect-to-another-php-file-after-closing-mysql/#findComment-958233 Share on other sites More sharing options...
cbr_600rr Posted November 16, 2009 Author Share Posted November 16, 2009 I was under the impression that this only works if you do that right away, and not run any forms. I tried using that earlier and did not have any luck. Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file. Quote Link to comment https://forums.phpfreaks.com/topic/181683-redirect-to-another-php-file-after-closing-mysql/#findComment-958240 Share on other sites More sharing options...
wee493 Posted November 16, 2009 Share Posted November 16, 2009 I was under the impression that this only works if you do that right away, and not run any forms. I tried using that earlier and did not have any luck. You could echo this, <script type="text/javascript"> <!-- window.location = "test2.php" //--> </script> This is just a redirect in Javascript. It won't give u an error. Quote Link to comment https://forums.phpfreaks.com/topic/181683-redirect-to-another-php-file-after-closing-mysql/#findComment-958246 Share on other sites More sharing options...
mikesta707 Posted November 16, 2009 Share Posted November 16, 2009 why not just put the form processing stuff on test2.php? you could also use a javascript or html redirect if you absolutely can't bring yourself to use a header() Quote Link to comment https://forums.phpfreaks.com/topic/181683-redirect-to-another-php-file-after-closing-mysql/#findComment-958249 Share on other sites More sharing options...
wee493 Posted November 16, 2009 Share Posted November 16, 2009 why not just put the form processing stuff on test2.php? you could also use a javascript or html redirect if you absolutely can't bring yourself to use a header() Ya, you could also have your form post to test2.php and put this at the top if ($_POST['form_name']){ // Do stuff // } Quote Link to comment https://forums.phpfreaks.com/topic/181683-redirect-to-another-php-file-after-closing-mysql/#findComment-958252 Share on other sites More sharing options...
cbr_600rr Posted November 16, 2009 Author Share Posted November 16, 2009 why not just put the form processing stuff on test2.php? you could also use a javascript or html redirect if you absolutely can't bring yourself to use a header() Because my test2.php is set to refresh every 15 minutes. So upon refresh I lose all my variables. Thats why I want to save my variable into my database on test1.php, then on test2.php I can start out by loading the variable out of the database and use it, then after the page refreshes, it can load it out of the database and use it again. I tried using javascript as well earlier this evening, and it would skip the mysql database code.... Im not sure why though. Quote Link to comment https://forums.phpfreaks.com/topic/181683-redirect-to-another-php-file-after-closing-mysql/#findComment-958256 Share on other sites More sharing options...
mikesta707 Posted November 16, 2009 Share Posted November 16, 2009 you should probably echo the javascript refresh with PHP in that case, but without seeing the code I can;t be of any help Quote Link to comment https://forums.phpfreaks.com/topic/181683-redirect-to-another-php-file-after-closing-mysql/#findComment-958259 Share on other sites More sharing options...
cbr_600rr Posted November 16, 2009 Author Share Posted November 16, 2009 you should probably echo the javascript refresh with PHP in that case, but without seeing the code I can;t be of any help ok here is the code, sorry its a bit sloppy at the moment, haven't had time to clean it up <form action="test1.php" method="post"><br> <p>user input: <input type="text" name="input" /><input type="submit" /></p> </form> <?php $test1var = $_POST['input'];?> //insert test1var into temporary database so I can refresh next page and pull it out <?php //server information $host="localhost"; // Host name $username="root"; // Mysql username $password="*****"; // Mysql password $db_name="db01"; // Database name $tbl_name="temp"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Insert data into mysql $sql="UPDATE $tbl_name SET tempvar ='$test1var'"; $result=mysql_query($sql); // if successfully insert data into database, displays message "Successful". if($result){ echo "</br></br>Successful"; } else { echo "ERROR"; } // close connection mysql_close(); ?> // AT THIS POINT I WOULD LIKE TO GO TO NEXT PHP FILE. //the next file begins like this.... ------------------------------------------------------ <meta http-equiv="refresh" content="300;url=test2.php"> <?php load mysql pull variable out of table use it close mysql and wait for refresh... **So basically I need to have that variable in my database before I come to this second page, otherwise upon the first refresh, the variable gets cleaned out. Quote Link to comment https://forums.phpfreaks.com/topic/181683-redirect-to-another-php-file-after-closing-mysql/#findComment-958275 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.