geroido Posted September 3, 2008 Share Posted September 3, 2008 Hi Can anyone please help me with this. I'm at my wits end. The code below is a validation page and an insertion page. If you go right down to the end of the page where I validate the contents of $_SESSION['content'](everything in between is irrelevant). If $_SESSION['content'] is empty, I throw the form back at the user else I insert the record. The problem is that if $_SESSION['content'] is empty, I return the user to this page and now the contents of $_SESSION['indwho'] are wiped because originally it came from a previous page. On reloading this page, the value of $_SESSION['indwho'] is erased. How can I keep this value. I'm pulling my hair out. Otherwise the code works well and will insert the record minus the contents of $_SESSION['indwho'] cos it has become empty. <?php session_start(); //Include database connection info and capture the users selection include("config.php"); $_SESSION['indwho'] = $_GET['id']; $_SESSION['content'] = $_POST['content']; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link href="default.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="header"> <div id="logo"> </div> <div id="menu"> <ul> <li class="active"><a href="userhome.php" title="">Homepage</a></li> <li><a href="aboutus.php" title="">About Us</a></li> <li><a href="cls_comments.php" title="">Comment</a></li> <li><a href="logout.php" title="">Logout</a></li> </ul> </div> </div> <div id="content"> <div id="sidebar"> <div id="support"> <h2>Client Support</h2> <h3>05991-64809</h3> </div> <div id="login" class="boxed"> <h2 class="title">Account Login</h2> <div class="content"> <form id="form1" method="post" action="userChoice.php"> <fieldset> <h3><?print $_SESSION['username']?></h3> You are now logged in.<br> <?include("interface.php");?> </fieldset> </form> </div> </div> <div id="updates" class="boxed"> <h2 class="title">Our job...</h2> <div class="content"> <ul> <li> <h3>Home delivery</h3> <p>Drinks and deliscious, piping hot meals delivered right to your doorstep.</p> </li> <li> <h3>Fast delivery</h3> <p>You can expect your orders to be delivered in double quick time. This is a pledge of our participating outlets.</p> </li> <li> <h3>Competitive prices</h3> <p>Our outlets strive to ensure highly competitve pricing.</p> </li> <li> <h3>Quality assurance</h3> <p>The best quality ingredients are sourced to create fantastic dishes.</p> </li> <li> <h3>Interactivity</h3> <p>An interactive website for users and clients.</p> </li> </ul> </div> </div> </div> <div id="main"> <div id="welcome" class="post"> <h2 class="title">Welcome <FONT color=#0066cc> <?print $_SESSION['username']?></FONT> to Online Ordering!</h2><BR> <h3 class="date"><span class="month"><?echo date("l, F d, Y h:i" ,time());?></h3><BR> <div class="meta"> <H3>You would like to send a comment to <FONT color=red><?echo $_SESSION['comment_to_who']?></FONT></H3><BR> <?php echo $_SESSION['indwho']; if (empty($_SESSION['content'])) { ?><H3>You <FONT color=red>must </FONT>enter a comment into the box provided</H3><BR> <form id="form1" method="post" action="cls_indcust.php"><BR><BR> <TABLE><TR><TD><textarea cols="80" rows="20" wrap="hard" name="content"> </textarea></TD></TR></TABLE><BR><BR> <input id="inputsubmit1" type="submit" name="send" value="Send" /></FORM><? }else{ $date = date("Y-m-d"); $time = date("H:i:s"); $insert = mysql_query("INSERT INTO `deliver`.`comments` (`userid` ,`recipientid` ,`comment` ,`date` ,`time` ,`forwho` ,`viewed`) VALUES ('".$_SESSION['userid']."', '".$_SESSION['indwho']."', '".$_SESSION['content']."', '".$date."', '".$time."', 'ind', 'no')") or die("Could not insert data because ".mysql_error()); echo $insert; if (!headers_sent()) { header("Location: cls_confirmcomment.php"); } else { $redirect = '<script type="text/javascript">window.location = "cls_confirmcomment.php";</script>'; $redirect .= '<noscript><meta type="refresh" content="0;url=cls_confirmcomment.php" /></noscript>'; echo $redirect; } } ?> </div> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/122520-solved-losing-session-variable-contents-on-page-rentry/ Share on other sites More sharing options...
geroido Posted September 3, 2008 Author Share Posted September 3, 2008 I,ve found the solution to my problem by adding the following code at the top of the page. This checks to see if the variable is set so it won't be overwritten with nothing. Else it gets the id. Thanks if (!isset($_SESSION['indwho'])){ $_SESSION['indwho'] = $_GET['id']; } Link to comment https://forums.phpfreaks.com/topic/122520-solved-losing-session-variable-contents-on-page-rentry/#findComment-632618 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.