PDA

View Full Version : Session Variables Nightmare


Daryl
11-18-2010, 08:15 AM
I am having a nightmare with session variables (again!)

I have tried using the native Dreamweaver Session Variable options to set a form field as a session variable and then on the next page to display it, with no luck.

I have also tried the ecart set session value also which I sometimes get to work.

I have created a single element form like this (using the ecart behaviour);

<?php
if (!session_id()) session_start();
if(($_SERVER["REQUEST_METHOD"] == "POST") && (isset($_SERVER["HTTP_REFERER"]) && strpos(urldecode($_SERVER["HTTP_REFERER"]), urldecode($_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"])) > 0) && isset($_POST)) {
$_SESSION["sessiontest"] = "".((isset($_POST["sessiontest"]))?$_POST["sessiontest"]:"") ."";
}
?>
<!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>
</head>

<body><form action="thankyou_pdf.php" method="post">
<p>
<input name="sessiontest" type="text" />
</p>
<p>
<input type="submit" name="test" id="test" value="Test" />
</p>
</form>
</body>
</html>

And then try and display it here;

<?php
if (!isset($_SESSION)) {
session_start();
}
?>
<!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>
</head>

<body>
<?php echo $_SESSION['sessiontest']; ?>
</body>
</html>

But I either get a blank page or a value that I had created in the form earlier in the day.

Can someone please tell me how to set a session variable from a form element using either a DW or Webassist behaviour and then show it on the next page?

Thanks in advance

Jason Byrnes
11-18-2010, 08:23 AM
is the form page named thankyou_pdf.php? or is thankyou_pdf.php the second page?


I ask this because the forms action is set to post to thankyou_pdf.php:
<form action="thankyou_pdf.php" method="post">


this is an important detail to note because all for processing must be performed on the forms action page.


you have the set session value code on the same page as the form, this will only work if the form is set to post to the same page that contains the form.

If the form is set to post to another page, the set session value code needs to be on that other page.

another important detail to note is the trigger. you have selected to use the Currant Page Submit trigger.

this trigger cannot be used if the form is set to post to another page.

Finally, make sure that all of your form elements have a values attribute, your text field is missing this:
<input name="sessiontest" type="text" />

some browsers will not post a form elements value if there is no value attribute:
<input name="sessiontest" type="text" value="" />

Daryl
11-19-2010, 11:13 PM
Hi Jason

I have got something to work now.

I used ecart to set a session on the form page, made the next page the form action, then cut the ecart code from the first page and pasted into the top of the second page, which seems to work.

Is this right?

The reason why I used page submit as a trigger was that I found if button pressed was used, in IE it sometimes doesn't recognise it, but with page submit it did.

Jason Byrnes
11-22-2010, 06:22 AM
you don't need to cut and paste the code.


if you have the form posting to another page, follow these steps:

On the action page, go to the bindings panel and click the plus button, select Form Data. Click the folder icon in the form data window and select your form page. This will add dynamic data bindings for the form to the forms action page.

Now on the action page, you can add the server behaviors using the form data bindings.