PDA

View Full Version : Using sessions to pass information


smitho
01-03-2008, 12:01 AM
I'm trying to create a process where a user has a file they upload and to do so a number of check boxes need to be selected. Once the check boxes and file has been uploaded the files meta data is extracted and presented to the user. I'd like to be able to give the user the option to edit some of the data extracted. For this I decided to go down the popup window method.

This is the test I put together.

boxesPage1.php is a simple selection of 4 check boxes when submitted the results a displayed on the second page.

boxesPage2.php shows the user the check boxes they selected and has an update link which opens a popup.

boxesPage3.php is the popup which looks similar to the choices presented to the user on the first page. The popup picks up the options selected and presents the user with the boxes either checked or unchecked depending on there first selection. Once they have made the change they click the submit button which takes them back to the main window (boxesPage2.php).

The problem I'm having is that when I click on the the update link and make a change to in the popup and submit it the change is not reflected in the main window. If I click the update link a second time and then just click the submit button on the popup window the results appear in the main window. Should it not occur the first time?

If anyone could suggest how to fix it so that the popup changes are applied to the main window the first time the popup is submitted it would be really appreciated.

boxesPage1.php

<?php

//Set the boxes to unchecked
$box1='unchecked';
$box2='unchecked';
$box3='unchecked';
$box4='unchecked';

?>

<!--create form-->
<form enctype="multipart/form-data" method="post" action="boxesPage2.php">
<table width="0%" border="0">
<tr>
<td><input type="checkbox" name="box1[]" value="box1"<?PHP print $box1; ?>>Checkbox1</td>
<td><input type="checkbox" name="box1[]" value="box2"<?PHP print $box2; ?>>Checkbox2</td>
<td><input type="checkbox" name="box1[]" value="box3"<?PHP print $box3; ?>>Checkbox3</td>
<td><input type="checkbox" name="box1[]" value="box4"<?PHP print $box4; ?>>Checkbox4</td>
</tr>
</table>
<br>
<br>
<input name="check" type="submit" id="send" value="Send File" />
<label></label>
</form>



boxesPage2.php

<?php

//start session to capture the choices made in the first screen and pass them on the update screen.
session_start();
if (empty($_SESSION['count'])) {
$_SESSION['count'] = 1;
} else {
$_SESSION['count']++;
}
echo "This session number is ".$_SESSION['count'];
echo "<br><br>";

if (isset($_POST["check"])) {

$box=$_POST["box1"]; //as a normal var
$box_count=count($box); // count how many values in array


foreach ($box as $cat) {
echo $cat." - ";
}


//check the array
echo "<hr>";

}
else //if the popup was used grab the options selected
{
$box = $_SESSION['boxes2'];
}
echo "<br>";

$_SESSION['boxes']=$box;

print_r($box);

?>
<!--display the choices-->
<form action="checkmeta.php" method="post" enctype="multipart/form-data" name="checklist">
<label>Choice
<input name="boxid" type="text" id="boxid" value="<?php foreach ($box as $cat) {echo $cat." - ";}?>" size="35" />
</label>
<a href="boxesPage3.php<?php echo htmlspecialchars(SID); ?>" onclick= "window.open('boxesPage3.php', 'window_name', 'width = 800, height = 250');return false;">UPDATE BOXES</a>
</form>


boxesPage3.php

<!--JS to close the popup and go back to the second screen-->
<script language="JavaScript">

function refreshParent() {
window.opener.location.href = window.opener.location.href;

if (window.opener.progressWindow)

{
window.opener.progressWindow.close()
}
window.close();
}

</script>
<?php
//get the choices made and shown in the second screen
session_start();
if (empty($_SESSION['count'])) {
$_SESSION['count'] = 1;
} else {
$_SESSION['count']++;
}
echo "This session number is ".$_SESSION['count'];
echo "<br><br>";

$box1='unchecked';
$box2='unchecked';
$box3='unchecked';
$box4='unchecked';

$box = $_SESSION['boxes'];

//check the boxes if they were checked or not.
foreach ($box as $value) {

if($value == 'box1'){
$box1='checked';
}
if($value == 'box2'){
$box2='checked';
}
if($value == 'box3'){
$box3='checked';
}
if($value == 'box4'){
$box4='checked';
}
}



?>

<form enctype="multipart/form-data" method="post" action="boxesPage2.php">
<table width="0%" border="0">
<tr>
<td><input type="checkbox" name="box1[]" value="box1"<?PHP print $box1; ?>>Checkbox1</td>
<td><input type="checkbox" name="box1[]" value="box2"<?PHP print $box2; ?>>Checkbox2</td>
<td><input type="checkbox" name="box1[]" value="box3"<?PHP print $box3; ?>>Checkbox3</td>
<td><input type="checkbox" name="box1[]" value="box4"<?PHP print $box4; ?>>Checkbox4</td>
</tr>
</table>
<br>
<input name="check" type="submit" id="send" value="Send File" onclick="refreshParent()" />
<label></label>
</form>

<?php
$_SESSION['boxes2']=$box;
?>

davidj
01-03-2008, 07:03 AM
The problem I'm having is that when I click on the the update link and make a change to in the popup and submit it the change is not reflected in the main window. If I click the update link a second time and then just click the submit button on the popup window the results appear in the main window. Should it not occur the first time?

it sounds like the process order is incorrect

recap...

you make a change in the popup and submit it but its not reflected in the main page yes?

if you refresh the main page manually does the change appear?

smitho
01-03-2008, 11:36 PM
You are correct davidj.
" you make a change in the popup and submit it but its not reflected in the main page yes?"

It seems that refreshing the page does not work.

I found a bit of code to help me see what's going on with my sessions.

Using this:

sessionCheck.php
<?php
session_start();
print "<br><pre>";print_r($_SESSION);print "</pre>";
?>I open this in a new window and my boxesPage1.php in another window.

I don't have a session start on boxesPage1.php so the sessionCheck.php page has nothing.

I tick box1 and click submit to go to page boxesPage2.php and see the result. Now on the sessionCheck.php page I have:

Array
(
[count] => 1
[boxes] => Array
(
[0] => box1
)

)

This is correct.

I click on the update link to take me to the pop. This is now session 2 so my sessionCheck.php page shows me:

Array
(
[count] => 2
[boxes] => Array
(
[0] => box1
)

[boxes2] => Array
(
[0] => box1
)

)

Which is also correct. The problem starts from here because when I click on box2 in the popup and then click on the submit button I'm taken back to the main window which only show box1 as the result and it tells me that I'm in session 3 but when I check my sessionCheck.php page I get:

Array
(
[count] => 4
[boxes] => Array
(
[0] => box1
[1] => box2
)

[boxes2] => Array
(
[0] => box1
)

)

Now the array is correct but it does not show this on the main page and I seem to have skip a session somewhere. So as mentioned before if I click on the update link a second time which opens the popup again this time with both box 1 & 2 checked the sessionCheck.php page shows:

Array
(
[count] => 5
[boxes] => Array
(
[0] => box1
[1] => box2
)

[boxes2] => Array
(
[0] => box1
[1] => box2
)

)

Which is correct. Not changing any of the boxes I click the submit button to go back to the main window again and this time the result is box1 and box2 are there but the session = 6 on the main page and on the sessionCheck.php page I have:

Array
(
[count] => 7
[boxes] => Array
(
[0] => box1
[1] => box2
)

[boxes2] => Array
(
[0] => box1
[1] => box2
)

)

Going back to session 2 which is when the popup open and I select box2 then submit it and then only see box1 in the results. If I refresh the page I still do not get box2 in the results and if I check sessionCheck.php page I get:

*Before refresh
Array
(
[count] => 4
[boxes] => Array
(
[0] => box1
[1] => box2
)

[boxes2] => Array
(
[0] => box1
)

*After refresh

Array
(
[count] => 5
[boxes] => Array
(
[0] => box1
)

[boxes2] => Array
(
[0] => box1
)

Hope this makes sense and sorry for the long response. Any ideas?

smitho
01-04-2008, 03:16 AM
EDIT:

Thought I worked it out but just stopped the popup closing and the result appeared in the popup rather than the main window.

smitho
01-04-2008, 04:05 AM
I've noticed that people test their pages on a number of browsers up until now I've just be using FireFox. I'm testing on both Mac and PC platforms (mainly Mac). I decide to try my pages in Safari and to my surprise it works great as it is. I did notice that where I have the session number being echoed on the main page it also jumps a session.

On FireFox the message I get after clicking the sumbit on the popup is:
This session number is 3

On Safari at the same stage I get
This session number is 4

I tried IE and it gave me that same result as FireFox.

Array
(
[count] => 4
[boxes] => Array
(
[0] => box1
[1] => box2
)

)

So is it that IE and FireFox are not refreshing?

davidj
01-04-2008, 06:48 AM
ok your doing something wrong here

lets look at your code

boxesPage3.php


<script language="JavaScript">

function refreshParent() {
window.opener.location.href = window.opener.location.href;

if (window.opener.progressWindow)

{
window.opener.progressWindow.close()
}
window.close();
}

</script>
<?php
//get the choices made and shown in the second screen
session_start();
if (empty($_SESSION['count'])) {
$_SESSION['count'] = 1;
} else {
$_SESSION['count']++;
}
echo "This session number is ".$_SESSION['count'];
echo "<br><br>";

$box1='unchecked';
$box2='unchecked';
$box3='unchecked';
$box4='unchecked';

$box = $_SESSION['boxes'];

//check the boxes if they were checked or not.
foreach ($box as $value) {

if($value == 'box1'){
$box1='checked';
}
if($value == 'box2'){
$box2='checked';
}
if($value == 'box3'){
$box3='checked';
}
if($value == 'box4'){
$box4='checked';
}
}



?>

<form enctype="multipart/form-data" method="post" action="boxesPage2.php">
<table width="0%" border="0">
<tr>
<td><input type="checkbox" name="box1[]" value="box1"<?PHP print $box1; ?>>Checkbox1</td>
<td><input type="checkbox" name="box1[]" value="box2"<?PHP print $box2; ?>>Checkbox2</td>
<td><input type="checkbox" name="box1[]" value="box3"<?PHP print $box3; ?>>Checkbox3</td>
<td><input type="checkbox" name="box1[]" value="box4"<?PHP print $box4; ?>>Checkbox4</td>
</tr>
</table>
<br>
<input name="check" type="submit" id="send" value="Send File" onclick="refreshParent()" />
<label></label>
</form>

<?php
$_SESSION['boxes2']=$box;
?>


this is very weird

you have PHP logic inside HTML output. Your problem is that your refreshing the parent page first (javascript) then peforming the update (PHP)

very strange layout

remember that logic should be above any output

smitho
01-04-2008, 11:40 AM
Thanks davidj,

I've very very new to web development I tackled JavaScript for In Design and Acrobat a couple of years ago and was look to take some of the stuff I'd done there further. So I ended up trying to learn PHP a couple of months ago.


you have PHP logic inside HTML output. Your problem is that your refreshing the parent page first (javascript) then peforming the update (PHP)

Not sure what you mean. Do I need to move the JS further down? I'll keep at it anyway.

Your time is very appreciated.

Thank you.

davidj
01-04-2008, 11:54 AM
structure is ...

PHP logic -> HTML Output

remember that javascript is not processed until fully loaded in the browser

The PHP is sent to the PHP engine and executed. Any variables and calculations are performed and then the results are output as HTML

PHP is alien to browsers and they wont render any. They only interpret HTML and Javascript

OK so first we write our PHP at the top of the document above the HTML tags. This keeps all in order and the universe keeps going! (rumours have it someone placed their PHP at the bottom of the page and ran it! The result was a big flash and the creation of matter)

then have your HTML

then in your head tags have your Javascript

its the natural order of things

just don't go into the dark!

you have all seen the Film EVENT HORIZON?

That ship fell into the void because of som dodgy PHP!

Its the truth

smitho
01-04-2008, 12:11 PM
That's interesting I'll give it a go tomorrow as it's late here and I can't keep my eyes open. I've seen EVENT HORIZON and always thought that some dodgy PHP could been the cause. Dam newbie coders.

Cheers davidj.

smitho
01-06-2008, 12:03 PM
OK I thought I followed what you suggested davidj but I obviously still don't get it.

Am I heading in the right direction?

<?php
//get the choices made and shown in the second screen
session_start();

//count the sessions
if (empty($_SESSION['count'])) {
$_SESSION['count'] = 1;
} else {
$_SESSION['count']++;
}
//display the session number
echo "This session number is ".$_SESSION['count'];
echo "<br><br>";
//set boxes to unchecked
$box1='unchecked';
$box2='unchecked';
$box3='unchecked';
$box4='unchecked';

$box = $_SESSION['boxes'];

//check the boxes if they were checked or not.
foreach ($box as $value) {

if($value == 'box1'){
$box1='checked';
}
if($value == 'box2'){
$box2='checked';
}
if($value == 'box3'){
$box3='checked';
}
if($value == 'box4'){
$box4='checked';
}
}
//the results of this sessions boxes are set to boxes2 and picked up in boxesPage2.php
$_SESSION['boxes2']=$box;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<!--JS to close the popup and go back to the second screen-->
<script language="JavaScript">

function refreshParent() {
window.opener.location.href = window.opener.location.href;

if (window.opener.progressWindow)

{
window.opener.progressWindow.close()
}
window.close();
}

</script>
</head>

<body>
<form enctype="multipart/form-data" method="post" action="boxesPage2.php">
<table width="0%" border="0">
<tr>
<td><input type="checkbox" name="box1[]" value="box1"<?PHP print $box1; ?>>Checkbox1</td>
<td><input type="checkbox" name="box1[]" value="box2"<?PHP print $box2; ?>>Checkbox2</td>
<td><input type="checkbox" name="box1[]" value="box3"<?PHP print $box3; ?>>Checkbox3</td>
<td><input type="checkbox" name="box1[]" value="box4"<?PHP print $box4; ?>>Checkbox4</td>
</tr>
</table>
<br>
<input name="check" type="submit" id="send" value="Send File" onclick="refreshParent()" />
<label></label>
</form>
</body>
</html>

smitho
01-06-2008, 10:29 PM
I must be getting closer because the refresh seems to be work now. When I change the options in the popup and submit them they still don't appear in the main window but they do if I refresh the page. This was not working before.

davidj
01-06-2008, 10:50 PM
ok

you need to refresh the page after you have performed the update

remamber that the code is actioned from top to bottom so this should help when setting a procedural event

so perform the update then do the refresh

smitho
01-06-2008, 11:16 PM
Hi davidj,

I think I got it but just want to be sure.

The key was to put this line in:

<body onunload="window.opener.location.reload()">

Have I got it correct?

EDIT: Just tried to access the page on my PC using FireFox and I got this message.

The page you are trying to view contains POSTDATA. If you resend the data, any action the form carried out (such as a search or online purchase) will be repeated. To resend the data, click OK. Otherwise, click Cancel.

FireFox and Safari on the Mac where I'm testing this works fine and IE on the PC works fine. Only Firefox on the PC gives me this message.



<?php
session_start();//get the choices made and shown in the second screen

if (empty($_SESSION['count'])) {
$_SESSION['count'] = 1;
} else {
$_SESSION['count']++;
}
echo "This session number is ".$_SESSION['count'];
echo "<br><br>";


$box1='unchecked';
$box2='unchecked';
$box3='unchecked';
$box4='unchecked';

$box = $_SESSION['boxes'];

//check the boxes if they were checked or not.
foreach ($box as $value) {

if($value == 'box1'){
$box1='checked';
}
if($value == 'box2'){
$box2='checked';
}
if($value == 'box3'){
$box3='checked';
}
if($value == 'box4'){
$box4='checked';
}
}
$_SESSION['boxes']=$box;
?>

<html>
<head>
<!--JS to close the popup and go back to the second screen-->
<script language="JavaScript">

function refreshParent() {
window.opener.location.href = window.opener.location.href;

if (window.opener.progressWindow)

{
window.opener.progressWindow.close()
}
window.close();
}
</script>
<title>Untitled Document</title>
</head>

<body onunload="window.opener.location.reload()">
<form action="boxesPage2.php" method="post" enctype="multipart/form-data" name="popcheck" id="popcheck">
<table width="0%" border="0">
<tr>
<td><input type="checkbox" name="box1[]" value="box1"<?php print $box1; ?>>Checkbox1</td>
<td><input type="checkbox" name="box1[]" value="box2"<?php print $box2; ?>>Checkbox2</td>
<td><input type="checkbox" name="box1[]" value="box3"<?php print $box3; ?>>Checkbox3</td>
<td><input type="checkbox" name="box1[]" value="box4"<?php print $box4; ?>>Checkbox4</td>
</tr>
</table>
<p><br>
<input name="check" type="submit" id="send" value="Send File" onclick="refreshParent()" />
</p>
</form>
</body>
</html>

davidj
01-06-2008, 11:17 PM
is it working?

davidj
01-06-2008, 11:19 PM
it would be better to use an onload

also i would remove the JS function as its not being used in your example

smitho
01-07-2008, 12:11 AM
Did a search on the web for the message I got and one site suggested using GET rather then POST may be better. When I did this and went to test the page it did not work. I decided to change back to the POST method and strangely enough the message I was getting on the PC started to appear on the Mac. I took out the onunload="window.opener.location.reload()" did the test again the message disappeared on the Mac but did not update the results page (as expected) put the onunload line back in and did the test again FireFox on the Mac worked fine PC version was giving the message again.

As it is it's working in IE on the PC and Safari, not working in Firefox.

it would be better to use an onload
I'll look into onload if you think it's better. Will this work with the popup?

also i would remove the JS function as its not being used in your example
The submit button calls the funtion [onclick="refreshParent()"] or is it because I put the onunload="window.opener.location.reload()"?

davidj
01-07-2008, 12:14 AM
or is it because I put the onunload="window.opener.location.reload()"?

yes its because your calling it there

smitho
01-07-2008, 12:55 AM
This is really giving me a headache. I changed the onclick="refreshParent()" to onclick="window.close()" because although I have the <body onUnload="window.opener.location.reload()" > it would not close the popup. The interesting this was that I did not need to function as you suggested but I'm still getting that message on FireFox.

So far the onclick="refreshParent()" seems to be better (no message in FireFox) but it just does not seem to reload the parent window properly. If I didn't get the POSTDATA message in FireFox the onunload method would be fine.

I'm not sure which method I should be trying to get to work.

smitho
01-07-2008, 03:27 AM
Must remember to clear the cookies when testing.

Changing the onclick="refreshParent()" to onclick="window.close()" will actually through a message similar to the one in FireFox.

The page cannot be refreshed without resending the information. Click Retry to send the information again, or click Cancel to return to the page that you were trying to view.

It seems I have to keep the refeshParent() function in to get the page to work properly. Strange to both IE and Safari work fine just FireFox does like it. Just grabbing the latest version of FireFox to see if that will make the difference.

davidj
01-07-2008, 06:52 AM
its down to the event

see which one is universal

smitho
01-07-2008, 09:38 PM
Sorry davidj,

what do you mean by "see which one is universal"?

davidj
01-07-2008, 09:52 PM
some events had problems with certain browsers

http://www.quirksmode.org/js/events_compinfo.html

its ok now though on the whole

smitho
01-07-2008, 10:42 PM
Thanks davidj,

There doesn't seem to be any test pages for the window.opener.location. which i think where the problem might be. I'll try search on web to see if FireFox has an issue with this.

Thanks for your help.