Jump to content

Archived

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

kamal213

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?

 

Thanks

Share this post


Link to post
Share on other sites

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.

Share this post


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)

Share this post


Link to post
Share on other sites

It seems that there is no telepath here :)

Show your code that redirect incorrectly. We'll think together.

Share this post


Link to post
Share on other sites

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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

sorry on the last part the header redirects to 'page_url' not 'page_id' was my typo!

Share this post


Link to post
Share on other sites

×
×
  • 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.