how to print a username in 2nd page using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • curi444
    New Member
    • Jan 2010
    • 18

    how to print a username in 2nd page using php

    i input a username in a textfield this is in 1st page
    Code:
    
    <?php
    		  if(isset($_POST['Submit']))
    		  {
    		  $username=$_POST['textfield'];
                      }
    ?>
    in next page i want to display this username in a form

    how to do?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Store the data in the session[1], or use query-string[2] data.

    Using the session:
    page1.php
    Code:
    <?php
    
    session_start();
    
    $_SESSION['username'] = "curi444";
    
    echo "<a href='page2.php'>Go to page 2</a>";
    
    ?>
    page2.php
    Code:
    <?php
    
    session_start();
    
    if (isset($_SESSION['username'])) {
        echo $username;
    }
    else {
        echo "No username set. <a href='page1.php'>Go tp page 1</a>";
    }
    
    ?>
    Using the query-string:

    page1.php
    Code:
    <?php
    
    $username = 'curi444';
    
    printf("Go to <a href='page2.php?username=%s'>page 2<a/>.",
           $username
    );
    
    ?>
    page2.php
    Code:
    <?php
    
    if (isset($_GET['username'])) {
        printf("Hello, %s.", $_GET['username']);
    }
    else {
        echo "No username. Go to <a href='page1.php'>page 1</a>.";
    }
    
    ?>
    [1] http://www.php.net/manual/en/book.session.php
    [2] http://www.php.net/manual/en/reserved.variables.get.php

    Comment

    Working...