...

View Full Version : Working with PHP sessions and form submitting - what am i missing here?



nikee
09-04-2009, 12:28 PM
Hey.

I'm a beginner to PHP and i'm currently trying to learn about sessions.

What i'm trying to do is to submit data from one page, save it in as a session variable and then navigate to another page and echo the session variable. But i believe i'm missing something, because it's just not working.

I get this error:
Parse error: parse error in C:\wamp\www\index2.php on line 3

I have two files. index.php and index2.php

index.php:


<?php
session_start();
$_SESSION['name']='';

if (isset($_POST['submit'])) {

$_SESSION['name'] = $_POST['name'];
echo 'The value of $_SESSION["name"] is now set to "' . $_SESSION['name'] . '"<br />';
echo '<a href="index2.php">Next Page</a>';

} else {
echo '
<form action="index.php" method="POST">
<input type="text" name="name" value="Nicklas Sandell">
<input type="submit" name="submit" value="Submit">
</form>
';
}
?>
index2.php


<?php
session_start()
echo 'The value of name is '.$_SESSION['name'];
?>
The error message says that the error is in index2.php and on line 3, and this is the code i have on that line:



echo 'The value of name is '.$_SESSION['name'];
So... can someone tell me what's going on here? I've been trying to find the error for so long now. It's so annoying!

Thanks.
-Nike

UD2006
09-04-2009, 12:45 PM
echo 'The value of name is '.$_SESSION['name'];



Try to remove the . before $_SESSION['name'];


Let me know if it works?

nikee
09-04-2009, 12:53 PM
Nope, same error.

Thanks for the respond. :)

I get the same error if i do this too:


session_start()
echo $_SESSION['name'];

UD2006
09-04-2009, 01:18 PM
Ok, try it like this:



<?php
session_start();
echo "The value of name is $_SESSION[name]";
?>

CFMaBiSmAd
09-04-2009, 01:19 PM
There is a missing ; on the end of the session_start() statement.

The php parser does not know what the programmer (you) intended on any line of code (which is why there are syntax rules to tell it.) So, it often reports errors on a line following the actual bad syntax because it can only get back "on track" when it finds something that is invalid in the current context. When something like a ; is missing to tell the parser where the end of a statement is it does not know that the following line(s) of code are NOT part of that statement until it encounters something that cannot possible be part of the open/missing syntax that it is parsing.

nikee
09-04-2009, 01:19 PM
Still the same error. :/

nikee
09-04-2009, 01:21 PM
Aaah, now it doesn't display that error anymore.. instead i got another one :/

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\index2.php on line 3

UD2006
09-04-2009, 01:22 PM
Still the same error. :/

If you try it exactly the way I post it, it should work, I tried it and I got the right result.

nikee
09-04-2009, 01:25 PM
Yeah, sorry UD2006. I only copied the second line cause i thought the error was there. But even if i add the ";" to the first line and use your code, it doesn't work, it just displays another error. The one i posted above.

But, if i use this:


<?php
session_start();
echo "The value of name is " . $_SESSION['name'];
?>


It works :)

Thanks for the help guys :D I appreciate it. It was just so annoying not being able to find the error. I sat there starring at the code for at least 20 minutes ^^

UD2006
09-04-2009, 01:29 PM
But, if i use this:


<?php
session_start();
echo "The value of name is " . $_SESSION['name'];
?>


It works :)


If you use:


<?php
session_start();
echo "The value of name is $_SESSION[name]";
?>


Instead of:


<?php
session_start();
echo "The value of name is " . $_SESSION['name'];
?>


You get the same result only you remove the two ' around name and the . before $_SESSION.

So you use a little less items ;)

nikee
09-04-2009, 01:34 PM
Oh now it works. I probably pasted it wrong last time. But i think i'll stick with using a dot between and those " ' " signs. It's easier to read. And it's easier to get confused if i don't use the dot and the " ' " signs, cause i can't see the syntax highlighting then..

Thanks though. Good to know i can use both ways :D

CFMaBiSmAd
09-04-2009, 01:56 PM
And, if you use {} around the array variable in the string, you don't even need to remove any of the '

echo "The value of name is {$_SESSION['name']}";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum