chemical_elii February 2016

how to store values in session properly

I have these pages that asks user for values. when I try to use the links to previous pages, the session values are gone and it requires user to go back to the first and repeat the process.

Here are sample codes based of my original code:

page1.php

<?php
 session_start();
 echo "<form method='POST' action='page2.php'>";
    echo "<input type='text' name='date1'>";
    echo "<input type='text' name='date2'>";
    echo "<input type='submit'>"
 echo "</form>";
?>

page2.php

<?php
 session_start();
 $_SESSION['date1'] = $_POST['date1'];
 $_SESSION['date2'] = $_POST['date2'];
      echo "<form method='POST' action='page3.php'>";
        echo "<input type='text' name='info1'>";
        echo "<input type='text' name='info2'>";
        echo "<input type='submit'>"
     echo "</form>";
echo "<a href='page1.php'>Change value in page 1</a>";
?>

page3.php

<?php
 session_start();
 $_SESSION['info1'] = $_POST['info1'];
 $_SESSION['info2'] = $_POST['info2'];
 if(isset($_POST['confirm'])){
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$db = 'sampDB';

$conn = mysqli_connect($mysql_host, $mysql_user, $mysql_pass, $db);

$query = mysqli_query($conn, 'INSERT INTO info(date1, date2, info1, info2)
                               VALUE ('$_SESSION[date1]', '$_SESSION[date2]', '$_SESSION[info1]', '$_SESSION[info2]')');

}
      echo "<form method='POST'>";
        echo $_SESSION['date1'];
        echo $_SESSION['date2'];
        echo $_SESSION['info1'];
        echo $_SESSION['info2'];
        echo "<input type='submit' name='confirm'>"
     echo "</form>";
echo "<a href='page1.php'>Change value in page 1</a>";
echo "<a href='page2.php'>Change value in page 2</a>";
?>

How do I go back to previos pages without destroying t

Answers


Ben February 2016

As long as you're not $_POSTing values back to the other pages, you can simply check if $_POST is set before overwriting values, like so:

session_start();
if(isset($_POST["date1"])) $_SESSION['date1'] = $_POST['date1'];
if(isset($_POST["date2"])) $_SESSION['date2'] = $_POST['date2'];

Reference: PHP isset


Pupil February 2016

Everytime, you are overwriting values of SESSION.

You need to check whether session is already set.

If already set, then there should be not change.

Also, posted values should be applied only if form is posted.

Add checks if sessions already exist like this:

$_SESSION['date1'] = !isset($_SESSION['date1']) ? $_POST['date1'] : 
$_SESSION['date1'];

$_SESSION['date2'] = !isset($_SESSION['date2']) ? $_POST['date2'] : $_SESSION['date2'];

Same for other session values.


makmesh February 2016

You should write your query this way

$query = mysqli_query($conn, "INSERT INTO info(date1, date2, info1, info2) 
VALUE ('{$_SESSION['date1']}', '{$_SESSION['date2']}', 
    '{$_SESSION['info1']}', '{$_SESSION['info2']}')");

Post Status

Asked in February 2016
Viewed 1,924 times
Voted 12
Answered 3 times

Search




Leave an answer