how to pass the variable through php header.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

amitdubey2
Forum Commoner
Posts: 48
Joined: Tue Apr 13, 2010 10:13 pm

Re: how to pass the variable through php header.

Post by amitdubey2 »

Hello Apollo,

It is working like this (Please check the comments in the code)

Code: Select all

<?php
include("includes/db.php");
include("includes/functions.php");

$cid= $_GET['cid']; //it is not working
$ci2= 1;

echo '<br>1a: '.$cid; // Here it is echoing the value  for $cid ...bt not in header
echo '<br>1b: '.$ci2; //Here it is echoing 1(its value)

if ($_REQUEST['command']=='add' && $_REQUEST['productid']>0)
{
        echo '<br>2a: '.$cid; // Here it is not echoing ...value
        echo '<br>2b: '.$ci2;  // Here it is echoing 1(its value)

        $pid=$_REQUEST['productid'];

        echo '<br>3a: '.$cid; // Here it is not echoing ...value
        echo '<br>3b: '.$ci2;//Here it is  echoing 1 (its value)

        addtocart($pid,1);

        echo '<br>4a: '.$cid; // here it  is not echoing ...value
        echo '<br>4b: '.$ci2;// here it is echoing 1(its value)

        header("Location: http://localhost/shopping/shoppingcart.php?cid=$cid");
      // (this won't work anymore because you have echo'd stuff)
        exit();
}
echo 'if you see this, no redirection would take place at all (neither with nor without $cid)';
?>
amitdubey2
Forum Commoner
Posts: 48
Joined: Tue Apr 13, 2010 10:13 pm

Re: how to pass the variable through php header.

Post by amitdubey2 »

Hello Apollo,

I am totally unable to find out whts wrong i am doing with this ...if you can help i can give you code which i am working on.One more guy in other forum had same problem but nobody was able to sort out there... :( :(
i guess this is only because of the addtocart function which is not allowing $cid to header to pass it through url to another page.. :idea: :roll:
Do you want to go for it:?: :?:



regards
amit :drunk:
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: how to pass the variable through php header.

Post by Apollo »

Well if it's already not outputting $cid at 2a, it's already messed up before the addtocart call.

What does this output:

Code: Select all

<?php
include("includes/db.php");
include("includes/functions.php");

$cid= $_GET['cid'];
$ci2= 1;

echo '<br>1a: '.$cid;
echo '<br>1b: '.$ci2;

if ($_REQUEST['command']=='add')
{
  echo '<br>2a: '.$cid;
  echo '<br>2b: '.$ci2;
}

if ($_REQUEST['productid']>0)
{
  echo '<br>3a: '.$cid;
  echo '<br>3b: '.$ci2;
}

echo '<br>4a: '.$cid;
echo '<br>4b: '.$ci2;

if ($_REQUEST['command']=='add' && $_REQUEST['productid']>0)
{
  $pid=$_REQUEST['productid'];
  addtocart($pid,1);
  header("Location: http://localhost/shopping/shoppingcart.php?cid=$cid");
  exit();
}

?>
If any of the $cid (1a, 2a, 3a, or 4a) is not being displayed, does it make a difference when you...
- comment the addtocart call ?
- comment the entire bottom if { } block?
- comment either or both includes at the top?
amitdubey2
Forum Commoner
Posts: 48
Joined: Tue Apr 13, 2010 10:13 pm

Re: how to pass the variable through php header.

Post by amitdubey2 »

Dear Apollo,

As i mentioned earlier...the header is not able to send the $cid only because of function addtocart.,,,,Because it is not able to echo $cid=$_GET['cid'] inside the if statement.
and there is no issue to comment the includes ...because than it will show warning and fatal errors(like call to undefined function like that and also database error)....
and if statement is necessary to respond the addtocart event..so can't exclude that also..
I think this is some bug in php itself... :dubious:

Is there any other way to pass the value inside the if statement which is calling that addtocart function.??? :?: :?:


Regards
Amit
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: how to pass the variable through php header.

Post by Apollo »

By running my test and commenting the things I mentioned you can simply pinpoint the place exactly where things go wrong.

The last code I posted, what did it output?
And can you try and comment the things I suggested? (I understand addtocart won't work if you comment the includes, so when the includes are commented then addtocart has to be commented as well obviously)

From the comment in your previous code, I understood the $cid variable is already lost even after just an IF statement (at 2a), i.e. before the addtocart is executed. So I'm curious to see if that still happens it addtocart isn't even in there.
amitdubey2
Forum Commoner
Posts: 48
Joined: Tue Apr 13, 2010 10:13 pm

Re: how to pass the variable through php header.

Post by amitdubey2 »

hello apollo,

first of all sorry for late replying ..bt i was nt well ..so couldnt reply..Anyways check this... :) :)
by Apollo
The last code I posted, what did it output?
As you said i am commenting Please check the code...

Code: Select all

<?php
include("includes/db.php");
include("includes/functions.php");

$cid= $_GET['cid'];
$ci2= 1;

echo '<br>1a: '.$cid; //  it is echoing the value for $cid in the same page bt nt thru header
echo '<br>1b: '.$ci2; // here it is echoing the value=1 and sending thru header

if ($_REQUEST['command']=='add')
{
  echo '<br>2a: '.$cid; //here it is not echoing any value and not sending thru header
  echo '<br>2b: '.$ci2; //here it is nt echoing value=1 bt after pressing addtocart button sending thru header 
} 

if ($_REQUEST['productid']>0)
{
  echo '<br>3a: '.$cid; //here it is not echoing any value
  echo '<br>3b: '.$ci2; //here it is nt echoing any value bt sending thru header after  pressing addtocart button 
}

echo '<br>4a: '.$cid;  //here it is echoing value for $cid bt nt sending thru header
echo '<br>4b: '.$ci2;  // here it is echoing value =1 and sending thru header after pressing   addtocart button.

if ($_REQUEST['command']=='add' && $_REQUEST['productid']>0)
{
  $pid=$_REQUEST['productid'];
  addtocart($pid,1);
  header("Location: http://localhost/shopping/shoppingcart.php?cid=$cid");
  exit();
}

?>



And when i am comenting the include and addtocart function call...(As i am using header outside the addtocart function call)..it is working fine ..sending value.like this...

Code: Select all

include("includes/db.php");
	include("includes/functions.php");
	
	$cid=$_GET['cid'];
	//echo $cid;
	if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
		$pid=$_REQUEST['productid'];
		addtocart($pid,1);
		
	}
	header("location:shoppingcart.php?cid=$cid");
		exit();

Now i think you can get my point...i am still in delimah..wht to do next :dubious: :dubious:



regards
amit..
Post Reply