php - how to store values in session properly -
i have these pages asks user values. when try use links previous pages, session values gone , requires user go first , repeat process.
here sample codes based of 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 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 go previos pages without destroying values session?
as long you're not $_post
ing values other pages, can check if $_post
set before overwriting values, so:
session_start(); if(isset($_post["date1"])) $_session['date1'] = $_post['date1']; if(isset($_post["date2"])) $_session['date2'] = $_post['date2'];
reference: php isset
Comments
Post a Comment