Jump to content

PHP Passing variables and header redirect


Recommended Posts

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?



Link to post
Share on other sites

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)

Link to post
Share on other sites

Kool here it goes:


			$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.


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

Link to post
Share on other sites

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).

Link to post
Share on other sites

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">
						<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>


$page_url= mysql_real_escape_string($_POST['page_url']);]


header("Location: $page_id");


And it works like a charm! Thanks for your help sergeiSS!

Link to post
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.