Moron Posted August 21, 2006 Share Posted August 21, 2006 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? Quote Link to comment Share on other sites More sharing options...
theblain Posted August 21, 2006 Share Posted August 21, 2006 I would pass $_Session variables.http://www.php.net/session Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 21, 2006 Share Posted August 21, 2006 [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 stuffecho "<a href='history.php?empnum=". $empnum. "'>history</a>";[/code] Quote Link to comment Share on other sites More sharing options...
Woolf Posted August 21, 2006 Share Posted August 21, 2006 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 Link to comment Share on other sites More sharing options...
Moron Posted August 23, 2006 Author Share Posted August 23, 2006 [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? Quote Link to comment Share on other sites More sharing options...
Moron Posted August 23, 2006 Author Share Posted August 23, 2006 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. Quote Link to comment Share on other sites More sharing options...
448191 Posted August 23, 2006 Share Posted August 23, 2006 Your second page is ok, the first is not.[code]<?phpsession_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 Link to comment Share on other sites More sharing options...
Moron Posted August 23, 2006 Author Share Posted August 23, 2006 [quote author=448191 link=topic=105102.msg420641#msg420641 date=1156339018]Your second page is ok, the first is not.[code]<?phpsession_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 = offsession.bug_compat_warn = 0I then stopped and restarted the Default Web Site, but the warning still comes back.Two steps forward, one step back! :) Quote Link to comment Share on other sites More sharing options...
448191 Posted August 23, 2006 Share Posted August 23, 2006 Directly assign $_SESSION['empcode'], there's no need to copy $empcode for it. Quote Link to comment Share on other sites More sharing options...
Moron Posted August 23, 2006 Author Share Posted August 23, 2006 [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. Quote Link to comment Share on other sites More sharing options...
lessthanthree Posted August 23, 2006 Share Posted August 23, 2006 [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. Quote Link to comment Share on other sites More sharing options...
Moron Posted August 23, 2006 Author Share Posted August 23, 2006 [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? Quote Link to comment Share on other sites More sharing options...
lessthanthree Posted August 23, 2006 Share Posted August 23, 2006 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.