Session Variable Woes..

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

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Session Variable Woes..

Post by facets »

Hi All,

Could anyone here point me in the right direction?
User : d11wtq has helped me alot! I thank him very much.

Basically i'm trying to retrive a stockId from a drop down menu (which is retrieved from mysql) then pass that to the popup to view all associated data with that stockId.

The popup works but no data is passed. The URL looks like :
http://127.0.0.1/materials_register/tes ... d&stockId=

And the 'hello world' is being printed on the parent page not the shild page.

tia, will

Code: Select all

<?php

include "includes/functions.inc";
include "includes/common_db.inc";

$link_id = db_connect($db);

global $PHP_SELF;
    
if ($_POST['submit']) {  
    
   echo '<pre>';
   print_r($_POST);
   echo "<h1>Submitted to DB</h1><br>"; 
   echo '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>';
   echo "</pre>";

} elseif ($_POST['viewDetails']) {
 
//Do whatever for second submit
 
 echo $stockId;
 echo $description;
 view();
 
 }
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<? $sql_query = mysql_query("SELECT description, stockId FROM austock");
	echo "<select name=\"stockId\">";
    	while(list($stockname, $stockId)=mysql_fetch_array($sql_query)) {
		$stockname = stripslashes($stockname);
		echo "<option value=\"$stockId\">$stockname</option>";
	}
	echo "</select>";
	mysql_free_result($sql_query);
?>
    
<br><br><input type="submit" name="submit" value="submit me!">
<br><br><input type="submit" name="viewDetails" value="View Details">
</form>

<?

function view() {
    echo "<script language=\"javascript\">var newWindow = window.open('".$_SERVER['PHP_SELF']."?action=view_record&stockId=".$stockId."')</script>";
    view_record($stockId) ;
    }

function view_record() {
 
    print "hello world <br>";
    echo $stockId ;
    print "<br>";

    print_r($_SESSION);
    
    }
?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. line 8 is useless.
  2. pass $stockId (which may or may not be set depending on your register_globals status which should be off) into view()
  3. view_record() is being called to write to the main page. The page you open should call it... also by passing the variable into it.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

I'm new to php, so please excuse my ignorance.

register_globals is off.

isn't this line passing stockId and calling view_record to the pop up?

Code: Select all

function view() {
    echo "<script language=\"javascript\">var newWindow = window.open('".$_SERVER['PHP_SELF']."?action=view_record&stockId=".$stockId."')</script>";
tias, Will./
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$stockId doesn't exist at that point. With register globals off, variables don't magically appear like that. $_POST['stockId'] should exist by that point. $description won't exist either. By the way, $stockname from lines 32, 33 and 34; isn't apart of your select query.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

ok..

so the code looks like this now.. (And is much leaner.. thanks.)
the stockId is not being passed still.
and i'm getting a copy of the main page in the popup.
\
any other tips?
thanks again.. Will.

Code: Select all

<?php

include "includes/functions.inc";
include "includes/common_db.inc";

$link_id = db_connect($db);

if ($_POST['submit']) {  
    
   echo '<pre>';
   print_r($_POST);
   echo "<h1>Submitted to DB</h1><br>"; 
   echo '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>';
   echo "</pre>";

} elseif ($_POST['viewDetails']) {
 
//Do whatever for second submit
    $_POST['$stockId'];
    view();
 }
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<? $sql_query = mysql_query("SELECT description, stockId FROM austock");
	echo "<select name=\"stockId\">";
    	while(list($stockId)=mysql_fetch_array($sql_query)) {
		echo "<option value=\"$stockId\">$stockId</option>";
	}
	echo "</select>";
	mysql_free_result($sql_query);
?>
    
<br><br><input type="submit" name="submit" value="submit me!">
<br><br><input type="submit" name="viewDetails" value="View Details">
</form>

<?

function view() {
    echo "<script language=\"javascript\">var newWindow = window.open('".$_SERVER['PHP_SELF']."?action=view_record&stockId=".$stockId."')</script>";
    }

function view_record() {
    print "hello world <br>";
    echo $stockId ;
    print "<br>";
    print_r($_SESSION);
    }
?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. you're not passing the stock id to the functions. Notice the difference of your call to print_r() and your call to view().
  2. in order to see the data passed to a function, the function must know that it will receive that data. Right now, view() expects nothing.
  3. the main page's code is showing because the script doesn't differenciate between popup and normal page. If you stuck the main page code inside a flow control like an if statement block, should help there.
Something to read: http://php.net/functions
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

So something like this?

Code: Select all

} elseif ($_POST['viewDetails']) {
 
//Do whatever for second submit
    view($_POST['$stockId']);
 }
?>
or

Code: Select all

} elseif ($_POST['viewDetails']) {
 
//Do whatever for second submit
    view($stockId);
 }
?>
tia, will.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

also, by removing

Code: Select all

while(list($stockname, $stockId)=mysql_fetch_array($sql_query)) {
        $stockname = stripslashes($stockname);
        echo "<option value=\"$stockId\">$stockname</option>";
We are now calling the stock by name and not stockId?
so when I want to pull the associated data for the stockId I will have to do a string compare to pull the records? Does that sound right?

tia, Will
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

PHP will see the stock ID, not the name.
Post Reply