1

I have a login.html page where the user logs in and their information; name and postcode displays. This information displays on the login.php page.

<?php
session_start();

echo "systemResult=Success";
        $row=mysql_fetch_array($result);
        echo $row['Name']." is located at".$row['Postcode'];
        $_SESSION['name'] = $row['User_id'];

I want this information to display in a form on a different page and not on the page its displaying on at the moment. (Shown below)

<form name="form1" method="post" action="info.php">
            <strong>info</strong>
            <br />
            <br />
            Name: <input name="name" type="text" id="name" />
            <br />
            Postcode: <input name="postcode" type="text" id="postcode" />
            <br />

            </form

            <div data-role="content">
            <?php
            session_start();
            echo $_SESSION['user_id'] 

I have set the session up on the login.php page but it still displays information on the wrong page. On the 'info' page, the information does display but not in the forms.

To sum up, I want the information that displays in login.php to display in info.php so when the user logs in, it automatically takes them to info.php, displaying their inforamtion.

The information is coming from the phpmyadmin database.

Thanks in advance

Info.php page

<!DOCTYPE html>
<?php
session_start();
echo $_SESSION['user_id'] 
?>
<html>

    </head>
    <body>
        <!-- Home -->
        <div data-role="page" id="page1">
            <div data-theme="a" data-role="header">
            <a data-role="button" data-theme="c" href="menu.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                    Main Menu
                    </a>
                <h3>
                    Your info
                </h3>
            </div>
            <form name="form1" method="post" action="login.php">
            <strong>Details</strong>
            <br />
            <br />
            Name: <input name="name" type="text" id="name" />
            <br />
            Postcode: <input name="postcode" type="text" id="postcode" />

            </form


         </div>
    </body>
</html>
4

3 回答 3

3

First off, your pages need to be .php

Secondly, you must start the session with session_start() before ANYTHING is rendered on the page. Start the session above the header at the beginning of the page.

Read up on PHP SESSIONS

UPDATE

Change your info.php code to look like this:

<?php
session_start();
// This needs to go before ANYTHING else on the page.
?>
<!DOCTYPE html>
<html>

    </head>
    <body>
        <!-- Home -->
        <div data-role="page" id="page1">
            <div data-theme="a" data-role="header">
            <a data-role="button" data-theme="c" href="menu.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                    Main Menu
                    </a>
                <h3>
                    Your info
                </h3>
                <p>
                    <?php // Put this where you want to echo the user_id on the page
                     echo $_SESSION['user_id']; ?>
                </p>
            </div>
            <form name="form1" method="post" action="login.php">
            <strong>Details</strong>
            <br />
            <br />
            Name: <input name="name" type="text" id="name" />
            <br />
            Postcode: <input name="postcode" type="text" id="postcode" />

            </form


         </div>
    </body>
</html>
于 2013-03-27T16:01:59.697 回答
0

My dear friend, I think you are confused with session variables. Please keep in mind that ,you should start

session_ start( );

at the very begning of you php page,before anything starts. You can store values to session variables as showm below

$_SESSION['demo'] = 'Hello';

and you can retrieve session variables as showm below

echo $_SESSION['demo'];

thats it. Double check u are getting values from db before assigning to session variable

于 2013-03-27T17:08:46.710 回答
0

Your session variable is called $_SESSION['name'], change it to $_SESSION['user_id'] = $row['User_id'] in your login.php file. Maybe you want multiple session variables? such as:

$_SESSION['user_id']  = $row['User_id'];
$_SESSION['name']     = $row['name'];
$_SESSION['postcode'] = $row['code'];

As already mention session_start() needs to be the done before anything else, move the php code to before the DOCTYPE.

于 2013-03-27T17:15:50.127 回答