Jump to content


Photo

Question about passing a variable.....


  • Please log in to reply
12 replies to this topic

#1 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 21 August 2006 - 08:17 PM

I have a form where someone enters their employee number, then it returns a page with leave history (some of you have already hashed this out with me).

Once the second page displays, I want them to be able to click for more history, BUT....I don't want them to have to re-enter their employee number.

So the question is, how do I pass an already-existing variable to the next page?



#2 theblain

theblain
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 21 August 2006 - 08:34 PM

I would pass $_Session variables.
http://www.php.net/session

#3 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 21 August 2006 - 08:35 PM

<?php
$empnum = $_GET['empnum']; // or POST however it got here ...

// do your database stuff for second page display


// link to history.php for more stuff
echo "<a href='history.php?empnum=". $empnum. "'>history</a>";

Legend has it that reading the manual never killed anyone.
My site

#4 Woolf

Woolf
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 21 August 2006 - 08:36 PM

You could use sessions.
  • Put session_start(); at the top of your pages
  • When someone submits the form, save the variable like this: $_SESSION['employee_num'] = $employee_num. Where $employee_num is the variable that you have when someone submits the form.
  • To recall the ID number on another page, use $_SESSION['employee_num'] (e.g. $employee_num = $_SESSION['employee_num'])


#5 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 23 August 2006 - 12:16 PM

You could use sessions.

  • Put session_start(); at the top of your pages
  • When someone submits the form, save the variable like this: $_SESSION['employee_num'] = $employee_num. Where $employee_num is the variable that you have when someone submits the form.
  • To recall the ID number on another page, use $_SESSION['employee_num'] (e.g. $employee_num = $_SESSION['employee_num'])


One question: I know that $_session_start(); has to be declared at the top of the page before any HTML code, but the variable I'll be using hasn't been defined yet at that point.

So.... can I put $_SESSION further down my page after the variable has been created or does it also have to be at the top?


#6 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 23 August 2006 - 12:34 PM

More info.....

On my first page, I have this at the top:

<?php 
session_start();  
$_SESSION['empcode']='$empcode';
session_register("empcode"); 
?>

On the second page, I have this at the top:

<?php 
session_start(); 
$empcode = $_SESSION['empcode'];
echo $empcode;
?>

On the second page called, where I put echo $empcode; as a test, it's displaying $empcode instead of displaying the value of $empcode.

If I echo it on the first page, it properly displays the employee number.


#7 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 23 August 2006 - 01:16 PM

Your second page is ok, the first is not.
<?php
session_start();
$_SESSION['empcode']=$empcode;
?>

'$empcode' doesn't work because everything within single quotes is treated as a string. Leave them. Also leave session_register(), you don't need it.

#8 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 23 August 2006 - 02:50 PM

Your second page is ok, the first is not.

<?php
session_start();
$_SESSION['empcode']=$empcode;
?>

'$empcode' doesn't work because everything within single quotes is treated as a string. Leave them. Also leave session_register(), you don't need it.


Many thanks! That worked.

Now, though, I'm getting....

Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0


I set the following two lines in the php.ini file as follows:

session.bug_compat_42 = off
session.bug_compat_warn = 0

I then stopped and restarted the Default Web Site, but the warning still comes back.

Two steps forward, one step back!

:)


#9 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 23 August 2006 - 03:04 PM

Directly assign $_SESSION['empcode'], there's no need to copy $empcode for it.

#10 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 23 August 2006 - 03:45 PM

Directly assign $_SESSION['empcode'], there's no need to copy $empcode for it.


That worked, too.  :)

My next hair-puller is to figure out how to pull records based on the passed $empcode variable.


#11 lessthanthree

lessthanthree
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationUK

Posted 23 August 2006 - 03:52 PM


That worked, too.   :)

My next hair-puller is to figure out how to pull records based on the passed $empcode variable.


Wouldn't that just be a simple SQL query?

mysql_query("SELECT * FROM history WHERE `empcode` = '".$_SESSION["empcode"]."'");

If you originally obtaining the empcode value from $_GET don't forget to clean the value and check the data is matching what you're expecting, else you're open to people injecting mallicious code into your script.
call me a safe bet, i'm betting i'm not

#12 Moron

Moron
  • Members
  • PipPipPip
  • Advanced Member
  • 368 posts

Posted 23 August 2006 - 04:01 PM




Wouldn't that just be a simple SQL query?

mysql_query("SELECT * FROM history WHERE `empcode` = '".$_SESSION["empcode"]."'");

If you originally obtaining the empcode value from $_GET don't forget to clean the value and check the data is matching what you're expecting, else you're open to people injecting mallicious code into your script.


I added that line to my query, so the query now reads....

$RESULTDS=mssql_query("SELECT DISTINCT LH.[Employee Number], LH.[Lmo], LH.[Lda], LH.[LYR], LH.[Hours], LH.[Leave Code], M2.[HRYRAT], M2.[EMPNO], M2.[MANLAP], M2.[MANLAC], M2.[MANLTC], M2.[MSKLAB], M2.[MSKLTC], M2.[MSKLAB], M2.[MSKLTC], M2.[NAMEMI], M2.[NAMEL], M2.[NAMEF] FROM LEAVHST LH INNER JOIN MASTERL2 M2 ON LH.[Employee Number]=M2.EMPNO WHERE M2.[EMPNO] = '".$_POST['employeenumber']."' and 'empcode' = '".$_SESSION["empcode"]."' ORDER BY LH.[LYR] desc, LH.[Lmo] desc, LH.[Lda] desc");
	$RESULT=mssql_fetch_assoc($RESULTDS);
	$HOURSRESULT=mssql_fetch_assoc($RESULTDS);



This section doesn't pull anything:

<CENTER>	
	
		<font size="3" color="#ff0000" face="arial"><b>

		<?php
		echo $RESULT['NAMEF'];
		?>
		
		<?php
		echo $RESULT['NAMEMI'];
		?>


		<?php
		echo $RESULT['NAMEL'];
		?>
		
		<?php
		echo " - ";
		?>

		<?php
		echo $RESULT['EMPNO'];
		?>

So what have I failed to do?


#13 lessthanthree

lessthanthree
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationUK

Posted 23 August 2006 - 04:06 PM

Unfortunately I know absolutely nothing about MsSQL, so I can't specifically point you to the right place, I would however suggest you check your query syntax. The empcode bit you've put in there is the only fieldname I can see with single quotes around it....I used backticks (`) in my query as that's what you use to define that it is a fieldname in MySQL...

Of course, it goes without saying to make sure you actually have a field named empcode in your table...

I'm also not sure why you skip in and out of PHP where you're echoing the $RESULT stuff when it could be in one php block...
call me a safe bet, i'm betting i'm not




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users