Forum Moderators: coopster

Message Too Old, No Replies

$_SESSION['variable'] wont = $anothervariable

can assign a "string" and any number

         

delboy1978uk

4:17 pm on Jun 22, 2006 (gmt 0)

10+ Year Member



hello geniuses worldwide! Total newb at PHP here

why wont this work?
scenario 1: assign session variables from a POST..

session_start();
$_SESSION['sessdateref'] = $_POST['dateref'];
$_SESSION['sessnoguests'] = $_POST['noguests'];
$_SESSION['sessguests'] = $_POST['guests'];

nothing works...
Scenario 2: Assign POST to variables then to SESSION

$noguests = trim ($_POST['noguests']);
$guests = trim ($_POST['guests']);
$date = trim ($_POST['dateref']);
session_start();
$_SESSION['sessdateref'] = $date;
$_SESSION['sessnoguests'] = $noguests;
$_SESSION['sessguests'] = $noguests;

nothing works...
Scenario 3: try a set string or number and not a variable

session_start();
$_SESSION['sessdateref'] = "why?";
$_SESSION['sessnoguests'] = $noguests;
$_SESSION['sessguests'] = 13;

output from : why? 13 (ie. no variable between)

to summarise:
= '$noguests'; gives me why? $noguests 13
= "$noguests"; gives me why? 13
= $noguests gives me why? 13
= $_POST['noguests']; gives me why? 13
the string "why?" always works
the number 13 always works
The single quoted 'strings' work
any $variable or $_POST wont work
the variables assigned from the POST print on the screen

if thats a bit muddled looking then im sorry i can give more details if you like

Please help!
Derek

eelixduppy

5:44 pm on Jun 22, 2006 (gmt 0)



What version of php do you have? Try adding
error_reporting(E_ALL);
to the top of the script. Also, use $HTTP_SESSION_VARS with PHP 4.0.6 or less(instead of $_SESSION).

Good luck!

Sekka

8:06 pm on Jun 22, 2006 (gmt 0)

10+ Year Member



Really daft question, but are you 100% sure the $_POST is set?

Try,

if (isset ($_POST['noguests'])) {
echo "I have been set!";
} else {
echo "I have NOT been set!";
}

delboy1978uk

8:25 pm on Jun 22, 2006 (gmt 0)

10+ Year Member



yeah they are definitely set!
the session variables are created on a page that displays the variables which are set from $_POST

they are accessed on the next page..

delboy1978uk

7:56 pm on Jun 27, 2006 (gmt 0)

10+ Year Member



any ideas anyone? i'm at a loss here
i have just transferred it across to my linux machine and it is doing the same thing so it is definitely a coding issue...

if you need to see anymore code i can post it on :-)
del

henry0

8:47 pm on Jun 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) A file receive a "_POST" from a form
for ex: $sessguests=$_SESSION['sessguests'];

2) To initiate a session from the same file:
$_SESSION['sessguests']=$sessguessts;
$sessguessts=$_SESSION['sessguests'];
//echo "test guests $sessguest";

3) to use it anywhere:
session_start();
$sessguessts=$_SESSION['sessguests'];

eelixduppy

8:51 pm on Jun 27, 2006 (gmt 0)



The only thing I can say is that you do not have the variables set to anything. Try echoing out the values to check:

[url=http://us2.php.net/manual/en/function.print-r.php]print_r[/url]($_POST);//prints out the _POST array

If the values are actually set correctly, then they should show here. For additional debugging add error_reporting [us2.php.net](E_ALL); to the top of the script. Make sure all of your spelling is correct too ;)

Good luck!

henry0

9:05 pm on Jun 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Like in my little demo
try to echo each step

Setup a new file with only one set of value
be sure to follow my example

BTW
is PHP ON? did you try phpinfo(); :)

eelixduppy

9:15 pm on Jun 27, 2006 (gmt 0)



blah (deleted comment)
;)

ehh..to not waste a post. PHP should be enabled because the session was set to a string value and echoed out correctly without error ;)

henry0

9:39 pm on Jun 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whats wrong with Humor

delboy1978uk

1:04 am on Jun 28, 2006 (gmt 0)

10+ Year Member



okay the full script for the pages are as follows:
(all this is done in dreamweaver btw)

a form filled in gets posted to the second page

the second page creates session cookies (supposedly), checks fields are filled in correctly or not, displays the forms contents for verification and has a submit button, which inserts the forms info into a record in mysql, and goes to the third page

the third page accesses the session cookies.

THE CODE FOR THE SECOND PAGE

<?php session_start();
$_SESSION['sessdateref'] = "why?";
$_SESSION['noguests'] = trim ($_POST['noguests']);
$_SESSION['sessguests'] = 7;
//get form info
$firstname= trim ($_POST['firstname']);
$lastname= trim ($_POST['lastname']);
$add1= trim ($_POST['add1']);
$add2= trim ($_POST['add2']);
$add3= trim ($_POST['add3']);
$city= trim ($_POST['city']);
$postcode = trim ($_POST['postcode']);
$country= trim ($_POST['country']);
$telephone= trim ($_POST['telephone']);
$email= trim ($_POST['email']);
$noguests = trim ($_POST['noguests']);
$guests = trim ($_POST['guests']);
$date = trim ($_POST['dateref']);

require_once('Connections/local_connection.php');
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc())? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue!= "")? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue!= "")? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue!= "")? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue!= "")? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue!= "")? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

$editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
$editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
}

if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO bookers (bookerfirstname, bookerlastname, bookeradd1, bookeradd2, bookeradd3, bookercity, bookerpostcode, bookercountry, bookertelephone, bookeremail, noguests, bookerguests) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($HTTP_POST_VARS['1st'], "text"),
GetSQLValueString($HTTP_POST_VARS['2nd'], "text"),
GetSQLValueString($HTTP_POST_VARS['ad1'], "text"),
GetSQLValueString($HTTP_POST_VARS['ad2'], "text"),
GetSQLValueString($HTTP_POST_VARS['ad3'], "text"),
GetSQLValueString($HTTP_POST_VARS['cit'], "text"),
GetSQLValueString($HTTP_POST_VARS['postc'], "text"),
GetSQLValueString($HTTP_POST_VARS['coun'], "text"),
GetSQLValueString($HTTP_POST_VARS['telph'], "text"),
GetSQLValueString($HTTP_POST_VARS['ema'], "text"),
GetSQLValueString($HTTP_POST_VARS['nog'], "int"),
GetSQLValueString($HTTP_POST_VARS['gsts'], "text"));

mysql_select_db($database_local_connection, $local_connection);
$Result1 = mysql_query($insertSQL, $local_connection) or die(mysql_error());

$insertGoTo = "choosehow2pay.php";
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?'))? "&" : "?";
$insertGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}?>

the actual body of the page is an if statement (if fields are empty, make user go back)(else make a form, show the values of the variables, and submit inserts to mysql then goes to the third page)

THE THIRD PAGES CODE

<?php session_start();
$noguests = $_SESSION['sessnoguests'];
$guests = $_SESSION['sessguests'];
$dateref = $_SESSION['sessdateref'];
?>

...and later...

<?php
print "<p>$dateref $noguests $guests</p>";
?>

it must be the dreamweaver method because i managed to make a form go to a blank page then a third page and display the contents but it doesnt seem to be doing it here

could it be anything to do with the fact that there is an insert to mysql on this page?

eelixduppy

1:15 am on Jun 28, 2006 (gmt 0)



It could be because of the fact that the third page should have
$noguests = $_SESSION['noguests'];
instead of
$noguests = $_SESSION['sessnoguests'];

I told you it may be in the spelling ;)

delboy1978uk

1:05 pm on Jun 29, 2006 (gmt 0)

10+ Year Member



ooops didnt see that but that aint the problem

I just tried the same thing on another site with the same result.
I think it has something to do with the fact that the second page creates the session variables but then does a dreamweaver record update

when i get to the third page the url has a? on the end with no data after it

its almost as if it thinks it is a new session or something
Any ideas?

delboy1978uk

2:48 pm on Jun 29, 2006 (gmt 0)

10+ Year Member



i just tried making a version with no dreamweaver record update on the second page - it worked! and the 3rd page url ends in

?PHPSESSID3486rvb8tioygargibberish

this definitely has something to do with it then
but how do we get round that? i take it we are all better off hand coding then instead of relying on dreamweavers method or have i stumbled across a limitation of php or mysql? i dunno please help, getting desperate here!