Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts

    Question Working with PHP sessions and form submitting - what am i missing here?

    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 Code:
    <?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 Code:
    <?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:

    PHP Code:
    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
    Last edited by nikee; 09-04-2009 at 01:36 PM.

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    218
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nikee View Post
    PHP Code:
    echo 'The value of name is '.$_SESSION['name']; 
    Try to remove the . before $_SESSION['name'];


    Let me know if it works?

  • #3
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    Nope, same error.

    Thanks for the respond.

    I get the same error if i do this too:
    PHP Code:
    session_start()
    echo 
    $_SESSION['name']; 

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    218
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Ok, try it like this:

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

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,147
    Thanks
    2
    Thanked 333 Times in 325 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    Still the same error. :/

  • #7
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    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

  • #8
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    218
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nikee View Post
    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.

  • #9
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    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 Code:
    <?php
    session_start
    ();
    echo 
    "The value of name is " $_SESSION['name'];  
    ?>
    It works

    Thanks for the help guys 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 ^^

  • #10
    Regular Coder
    Join Date
    Jul 2007
    Location
    Velsen Noord, Netherlands
    Posts
    218
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nikee View Post
    But, if i use this:
    PHP Code:
    <?php
    session_start
    ();
    echo 
    "The value of name is " $_SESSION['name'];  
    ?>
    It works
    If you use:
    PHP Code:
    <?php
    session_start
    ();
    echo 
    "The value of name is $_SESSION[name]"
    ?>
    Instead of:
    PHP Code:
    <?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

  • #11
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    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

  • #12
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,147
    Thanks
    2
    Thanked 333 Times in 325 Posts
    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']}";
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •