1

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 the values from the session?

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

I tried $_SESSION['date1'] = isset($_POSTS['date1']) ? $_POSTS['date1'] : ''; not sure why i did not work
@chemical_elii It won't work because if the $_POST value isn't set, it'll make the $_SESSION value blank. You don't want to affect the $_SESSION value if $_POST is not set!
0

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.

Comments

0

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']}')");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.