Jump to content

Archived

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

Moron

Question about passing a variable.....

Recommended Posts

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?

Share this post


Link to post
Share on other sites
[code]
<?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>";[/code]

Share this post


Link to post
Share on other sites
You could use sessions.
[list]
[*]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'])
[/list]

Share this post


Link to post
Share on other sites
[quote author=Woolf link=topic=105102.msg419650#msg419650 date=1156192586]
You could use sessions.
[list]
[*]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'])
[/list]
[/quote]

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?

Share this post


Link to post
Share on other sites
More info.....

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

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

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

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

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

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

Share this post


Link to post
Share on other sites
Your second page is ok, the first is not.
[code]
<?php
session_start();
$_SESSION['empcode']=$empcode;
?>[/code]

'$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.

Share this post


Link to post
Share on other sites
[quote author=448191 link=topic=105102.msg420641#msg420641 date=1156339018]
Your second page is ok, the first is not.
[code]
<?php
session_start();
$_SESSION['empcode']=$empcode;
?>[/code]

'$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.
[/quote]

Many thanks! That worked.

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

[quote][b]Warning:[/b] 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 [b]Unknown[/b] on line [b]0[/b]
[/quote]

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!

:)

Share this post


Link to post
Share on other sites
Directly assign $_SESSION['empcode'], there's no need to copy $empcode for it.

Share this post


Link to post
Share on other sites
[quote author=448191 link=topic=105102.msg420725#msg420725 date=1156345465]
Directly assign $_SESSION['empcode'], there's no need to copy $empcode for it.
[/quote]

That worked, too.  :)

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

Share this post


Link to post
Share on other sites
[quote author=Moron link=topic=105102.msg420761#msg420761 date=1156347916]

That worked, too.   :)

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

[/quote]

Wouldn't that just be a simple SQL query?

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

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.

Share this post


Link to post
Share on other sites
[quote author=lessthanthree link=topic=105102.msg420771#msg420771 date=1156348366]



Wouldn't that just be a simple SQL query?

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

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.
[/quote]

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

[code]$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);

[/code]


This section doesn't pull anything:

[code]
<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'];
?>
[/code]

So what have I failed to do?

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

×

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.