0

I working on the project and I need help with displaying what user typed in input on one page and display on the other page using session.

This is my code for page where user types his/her name:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

    <form action="s2q2.php" name="s2n" method="POST">
        <p>Enter your name:</p>
        <input style="border: none; border-bottom: dashed; height: 50px; width:25%; margin-top: 45px; font-size: 35px;" type="text" script="sessionStorage.setItem('s2n');"><br>
        <button class="button" type="submit" style="vertical-align: center; background-color: #1e00ff; margin-top: 50px;" name="s2q1"><span>Next </span></button>
    </form>

</body>
</html>
  • this code is transferring the variable using method POST to a site called s2q2.php. Can you also post this site? –  Sep 01 '17 at 21:43
  • Also, you reference [sessionStorage.setItem()](https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem), but I don't think `input` elements have `script` attributes. What is that intended to do? – showdev Sep 01 '17 at 21:49
  • 1
    [The manual](http://php.net/manual/en/function.session-start.php) for sessions is always a good place to start. – Qirel Sep 01 '17 at 21:58

4 Answers4

0

Your question is a bit confusing but from my experience working with PHP here's how we $_POST data and make use of $_SESSION the easy way

EDIT : in your question we don't know whether you know how to use session_start() or not. We also don't know how the files and folders in your program are set up to give proper answer. But usually (well me) I create a config.php file which holds database information then I include that file in the header (header.php) because usually header.php is included everywhere in the program. I created file examples assuming you are using mysqli.

config.php

if (session_status() == PHP_SESSION_NONE) {
 define('DB_SERVER', 'localhost');
   define('DB_USERNAME', 'root');
   define('DB_PASSWORD', 'root');
   define('DB_DATABASE', 'my_database_name');
   $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
}

    // This tells the web browser that your content is encoded using UTF-8 
    // and that it should submit content back to you using UTF-8 
    header('Content-Type: text/html; charset=utf-8'); 

    // This initializes a session.  Sessions are used to store information about 
    // a visitor from one web page visit to the next.  Unlike a cookie, the information is 
    // stored on the server-side and cannot be modified by the visitor.  However, 
    // note that in most cases sessions do still use cookies and require the visitor 
    // to have cookies enabled.  For more information about sessions: 
    // http://us.php.net/manual/en/book.session.php 
    session_start(); 

    // Note that it is a good practice to NOT end your PHP files with a closing PHP tag. 
    // This prevents trailing newlines on the file from being included in your output, 
    // which can cause problems with redirecting users.

header.php (include this file everywhere you need or else include config.php)

 header('Content-Type: text/html; charset=iso-8859-1'); 
 ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);//handle php errors
 ob_start();//prevent header from being sent twice
 require_once('config.php'); 

form

require_once('header.php'); 

<form action="s2q2.php" method="POST">
    <p>Enter your name:</p>
    <input type="text" name="name"><br>
    <button type="submit" name="s2q1"><span>Next </span></button>
</form>

s2q2.php

if (isset($_POST['s2q1'])) {     // Retrieve form
    $name = $_POST['name'];      // retrieve data name
    $_SESSION['name'] = $name;   // pass data in session
}

Using $_SESSION

if (isset($_SESSION['name'])) {
    echo $_SESSION['name'];
} else {
    //do something else;
}
Balloon Fight
  • 661
  • 8
  • 16
0

First, modify the form input to have a name :

I working on the project and I need help with displaying what user typed in input on one page and display on the other page using session.

This is my code for page where user types his/her name:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

     <form action="s2q2.php" name="s2n" method="POST">
         <p>Enter your name:</p>
         <input name="username" style="border: none; border-bottom: dashed; height: 50px; width:25%; margin-top: 45px; font-size: 35px;" type="text" script="sessionStorage.setItem('s2n');"><br>
         <button class="button" type="submit" style="vertical-align: center; background-color: #1e00ff; margin-top: 50px;" name="s2q1"><span>Next </span></button>
    </form>
</body>
</html>

then, on s2q2.php page:

<?php
session_start();

$_SESSION["username"] = $_POST["username"];
echo $_SESSION["username"] ;
?>
Qirel
  • 25,449
  • 7
  • 45
  • 62
M. Waheed
  • 877
  • 1
  • 8
  • 8
0

First you create a session file called session_start.php that looks like this

 <?php
   session_start();
 ?> 

then you just need to include it everywhere you want to have your session variables

<!DOCTYPE html>
<html>
<head>
    <title>Input Site</title>
    <style>
    .input1 {
            border: none;
            border-bottom: dashed;
            height: 50px; width:25%;
            margin-top: 45px;
            font-size: 35px;
           }
    .button1 {
            vertical-align: center;
            background-color: #1e00ff;
            margin-top: 50px;
           }
    </style>
</head>
<body>

<form action="" method"post">
<p>Enter your name:</p>
<input class="input1" type="text" name="nameInput"><br>
<button class="button1" type="submit" name="submitBut"><span>Next</span></button>
</form>

<?php 
// including the session file
require_once("session_start.php");

if (isset($_POST['submitBut'])) { 
 $_SESSION['nameInput'] = $_POST['nameInput'];
} 
?> 
</body>
</html>

then you create whatever site whatever.php and just have include your session_start.php file

<!DOCTYPE html>
<html>
<head>
    <title>Whatever Site</title>
</head>
<body> 
<?php 
// including the session file
require_once("session_start.php");  
?>  
<strong><?php echo $_SESSION['nameInput'];?></strong>
</body>
</html>
  • `session_start()` shouldn't be after any output, it might not be started if it is - see https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php - and I honestly see no reason why to have it in a separate file if that's all it does. (And nitpicking: `require` isn't a function) ;-) – Qirel Sep 01 '17 at 22:20
  • there is no output after session_start() in session_start.php. –  Sep 01 '17 at 22:23
  • But there is before you use it. You have HTML before including the file. – Qirel Sep 01 '17 at 22:24
  • There is no html in session_start.php –  Sep 01 '17 at 22:30
  • That's right - but there is output before you require that file, and so there becomes output before the session call. And might I ask why you have it in a separate file, that does nothing more than start the session? Seems like more work than it saves to me! ;-) – Qirel Sep 01 '17 at 22:33
0

Oh well, lets see another example ( need to sanitize $name before output! )

<?php
    $name = isset($_SESSION['key_whatever']) ? $_SESSION['key_whatever'] :null;
?>

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>

        <form action="s2q2.php" method="POST">
            <p>Enter your name:</p>
            <input type="text" name="name" value="<?=$name?>">
            <br>
            <button type="submit" name="s2q1"><span>Next </span></button>
        </form>

    </body>
    </html>





/*

    on submit if youre not using any rewrite rules -> s2q2.php will be compiled and executed

        -------s2q2.php-------
*/
<?php    
     session_start();   <--- need to be set
     $_SESSION['key_whatever'] = isset($_POST['name']) ? $_POST['name'] : null;

?>