Page 1 of 1

Session Variable Woes..

Posted: Sun Apr 17, 2005 6:59 am
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);
    
    }
?

Posted: Sun Apr 17, 2005 7:11 am
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.

Posted: Sun Apr 17, 2005 7:24 am
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./

Posted: Sun Apr 17, 2005 7:31 am
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.

Posted: Sun Apr 17, 2005 7:41 am
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);
    }
?

Posted: Sun Apr 17, 2005 7:53 am
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

Posted: Sun Apr 17, 2005 8:37 am
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.

Posted: Sun Apr 17, 2005 9:12 am
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

Posted: Sun Apr 17, 2005 9:26 am
by feyd
PHP will see the stock ID, not the name.