Page 1 of 1
open new window while passing session variable, how?
Posted: Sat Sep 15, 2007 11:13 am
by jimandy
I'm working on a project involving a database of names and associated information. I have a working set of files that let me display all records found matching an entry of a last_name.
I'm probably doing things the hard way but they work.. Here's the scenario in abbreviated form...
1. input form page script
Code: Select all
<form method=GET action='GO.php'>
<input type='text' name='last_name' method='GET'>
<input type ="submit" value="Do it">
etc...
2. intermediate script – GO.php
Code: Select all
<?
session_start();
$_SESSION['last_name'] = $_GET[last_name]; // need this for use in script #3
header( 'Location: /show_results.php' ) ;
?>
3. show results page script
[after connecting to mysql]
Code: Select all
$last_name= $_SESSION['last_name];
[ I then build a query to mysql using $last_name, etc.]
The #1 script lets me input some last_name and when submitted calls script #2 which stuffs the GET data into the session variable and then loads script #3 which looks up the last_name in mysql and displays and related fields. However script #3 is opening in the same window and I would like it to open in a new window. I understand that the header function won't do that and I need to use JavaScript to open a new window but not sure where to put what. Also, I think I should be able to combine scripts 1 and 2 into one script but not sure how.
Help would be appreciated.
Yes, I know I could avoid script 2 by passing the GET vartble directlly to script 3. If asked I'll explain why I have to use the session variable.
scottayy | Please use the Code: Select all
and [syntax..] tags when posting code.[/color][/b]
Posted: Sat Sep 15, 2007 1:41 pm
by lafflin
first off I should tell you that I'm a beginner myself, but I miught be able to help with some of your questions.
to combine your scripts, it's not difficult, you just need to have the action value wether it be POST or GET method pointing back to the same script. There are a few advantages to this on being you can start making sticky forms, although you could make sticky forms using a second script too I guess, but it'd be alot of extra work. As far as the layout with this technique, you just controll which proccessing you want to go through with IF statements, often times checking the value of a hidden input. for instance if you create a
Code: Select all
<input type"hidden" name="submitted">
and then in your script you could have
Code: Select all
IF (isset($_GET['submitted'])) { Do All of this ;}
else
{do all of this ;}
echo' here you echo out all your html and java which will be displayed either way'
I don't know how to open in another window using the Header function, but I don't know js, I'm a cut and paster.
You could however display the result in the same page if you combine your scripts. to do so just echo out the table and the results within a for each array here's an example:
Code: Select all
if ($result) { // If it ran OK, display the records.
// Table header.
echo //'<table align="center" cellspacing="0" cellpadding="5">
'<tr height ="4px" bgcolor ="#bbbbbb"><td colspan="11"></td></tr>';
// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr height="2px" bgcolor="#000000"><td colspan ="12"></td></tr>
<tr bgcolor="#770000">'
. '<td align ="center">'.$row['sid'].'</td>
<td><font color ="#000000">::</font></td>
<td align="center">'.$row['first_name'] . '</td>
<td><font color ="#000000">::</font></td>
<td align="center">' . $row['last_name'] .'</td>
<td><font color ="#000000">::</font></td>
<td align="center">'.$row['name']. '</td>
<td><font color ="#000000">::</font></td>
<td align ="center"> DOB: '.$row['dob'].'</td>
<td><font color ="#000000">::</font></td>
<td align ="center">'.$row['sex'].'</td>
<td align ="center">' ;
$_SESSION['url'] = 'view_users.php?'.$nextquery ;
echo '<a href="view_edit.php?sid=' . $row['sid'] .'">VIEW</a> </td>';
echo ' </tr>
<tr bgcolor="#700000">
<td align="center"><P> ' . $row['parent_name'] . '</P></td>
<td><font color ="#000000">::</font></td>
<td align="center"><p>Registered: ' . $row['reg_date'] . '</p></td>
</tr>';
}
//echo '</table>';
mysql_free_result ($result); // Free up the resources.
mysql_close(); // Close the database connection.
there's some extra stuff in there, but this is a working copy from one of my scripts. the extra <td>'s in there are just for design.
hope this helps
Posted: Sun Sep 16, 2007 1:29 am
by joncampbell
You might wanna try passing the PHPSESSID to the new script, that should work as follows
Code: Select all
<?
session_start();
$sid = session_id();
$_SESSION['last_name'] = $_GET[last_name]; // need this for use in script #3
header( 'Location: /show_results.php?PHPSESSID=' . $sid ) ;
?>
but one question I have is why store it in a session, why not just pass it as a GET variable to the 3rd script, like this:
Code: Select all
<?
header( 'Location: /show_results.php?last_name=' . $_GET[last_name]) ;
?>
if you post some of script 3 I might be able to tell you what the $_GET code would be instead of a $_SESSION assignment.
Posted: Tue Sep 18, 2007 10:42 am
by jimandy
Jon, the script that displays the found records creates 'next' and 'previous' links and page numbers in between when the number of found records exceeds the preset limit of 10. It calls itself each time a 'next' or aanother page number is clicked. Since it calls itself, the GET data seems to be cleared, I suppose because when a script runs, all standard variables are reset (I think). Therefore I have to have the object of my query that was submitted earlier from the form page available to use over and over as I click on 'next'. Does this make sense? Obviously I am a newbie to PHP.
Posted: Tue Sep 18, 2007 4:14 pm
by nhammond
if you set session variables and stay within the same domain you can access them from any php file, you just need to make sure you have a session_start() at the top of the script or before any calls to a session variable.
set session
Code: Select all
session_start();
$_SESSION['uid'] = 'the-user-id';
read session
Code: Select all
session_start();
$user_id = $_SESSION['uid'];
echo $user_id;