kamal213 Posted December 6, 2011 Share Posted December 6, 2011 Hi guys I have 2 question. 1. Is it possible to use the a variable in a header redirect function i.e. $test = 'index.php'; header("location: $test"); 2 assuming $test = 'index.php'; how do I pass the variable from one page to another? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/252576-php-passing-variables-and-header-redirect/ Share on other sites More sharing options...
SergeiSS Posted December 6, 2011 Share Posted December 6, 2011 1. Yes, it's possible. 2. You may use GET variables or SESSION variables. 2a: $test = 'index.php?id=10'; header("location: $test"); 2b: read about sessions. Quote Link to comment https://forums.phpfreaks.com/topic/252576-php-passing-variables-and-header-redirect/#findComment-1294911 Share on other sites More sharing options...
kamal213 Posted December 6, 2011 Author Share Posted December 6, 2011 Thanks for your reply I successfully pass the variable using the $_GET! However I cant get it to redirect to the $test, it redirects to my home page instead! (p.s my index.php isn't my home page I use it for something else..weird but thats the case) Quote Link to comment https://forums.phpfreaks.com/topic/252576-php-passing-variables-and-header-redirect/#findComment-1294926 Share on other sites More sharing options...
SergeiSS Posted December 6, 2011 Share Posted December 6, 2011 It seems that there is no telepath here Show your code that redirect incorrectly. We'll think together. Quote Link to comment https://forums.phpfreaks.com/topic/252576-php-passing-variables-and-header-redirect/#findComment-1294950 Share on other sites More sharing options...
kamal213 Posted December 6, 2011 Author Share Posted December 6, 2011 Kool here it goes: <?php $x = $_SERVER['REQUEST_URI']; preg_match('/\/[a-z0-9]+.php/', $x, $match); $page = array_shift($match); $page = ltrim($page, '/'); global $page; ?> The above code gets the current page's url and strips it to just the file name i.e. index.php <a href="customers.php?id=' . $c_id . '&rid=' . $page . '"> ' . $c_name . '</a> I the pass the file name in the above variable. session_start(); if(isset($_GET['id']) && $_GET['rid']){ // Connect to the MySQL database include "leadscript/connect_to_mysql.php"; //This will check to see the URL variable is set and it exists in the db $check_id = preg_replace('#[^0-9]#i', '', $_GET['id']); $page_url = $_GET['id']; if(isset($_POST['submit'])) { $check_id = mysql_real_escape_string($_POST['thisID']); $c_name = mysql_real_escape_string($_POST['c_name']); $c_pbo = mysql_real_escape_string($_POST['c_pbo']); $c_callbackcomplete = mysql_real_escape_string($_POST['c_callbackcomplete']); $submit = mysql_real_escape_string($_POST['submit']); $insert=mysql_query("UPDATE customer SET c_name= '$c_name', c_pbo='Dead', c_callbackcomplete= 'COMPLETE', c_pbodate=now() WHERE c_id='$check_id' ")or die(mysql_error()); header("location: $page_url"); } Then the above code post to my database table - Its posts to the database fine, but just keeps redirecting to my home page instead hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/252576-php-passing-variables-and-header-redirect/#findComment-1294960 Share on other sites More sharing options...
SergeiSS Posted December 6, 2011 Share Posted December 6, 2011 OK. Instead of trying header("location: $page_url"); write here echo $page_url and check what do you have here. Until now I'm not sure that you have in this variable the correct URL (the URL that you like to have here). Quote Link to comment https://forums.phpfreaks.com/topic/252576-php-passing-variables-and-header-redirect/#findComment-1294979 Share on other sites More sharing options...
kamal213 Posted December 7, 2011 Author Share Posted December 7, 2011 YES!! Iz iz working yah!!! lol its ok! What I had to do was create a hidden input field in my posting form, make sure is set by defining a post variable, the use the post variable in my header function like so: <form action="deadleads.php" id="form2" name="form2" method="POST" target="_self"> <table> <tr><td colspan="2"><input name="page_url" type="hidden" value="' . $page_url . '" /></td></tr> <tr><td><input type="submit" name="submit" value="Continue"/></td></tr> </table> </form>'; $page_url= mysql_real_escape_string($_POST['page_url']);] header("Location: $page_id"); And it works like a charm! Thanks for your help sergeiSS! Quote Link to comment https://forums.phpfreaks.com/topic/252576-php-passing-variables-and-header-redirect/#findComment-1295211 Share on other sites More sharing options...
kamal213 Posted December 7, 2011 Author Share Posted December 7, 2011 sorry on the last part the header redirects to 'page_url' not 'page_id' was my typo! Quote Link to comment https://forums.phpfreaks.com/topic/252576-php-passing-variables-and-header-redirect/#findComment-1295213 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.