fishbaitfood Posted July 29, 2010 Share Posted July 29, 2010 So, I have two forms.. One for displaying a submitted name. And one for displaying an array of submitted words. At the very top of my index.php, I have: <?php session_start(); And only in my Second form, for displaying array of words, I've put a 'clear' button to clear the array, which works : elseif (isset($_POST['reset'])) { $_SESSION['words'] = ''; unset($_SESSION['words']); session_destroy(); } Problem: When I've submitted a name in the First form, and then submit a word in the Second form, the name of the First form disappears! This is also happening the other way, as well as using the reset button for the Second form. Should I use Two Sessions? Is this possible? And how do I do it? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/209214-session-interrupting-other-input-form/ Share on other sites More sharing options...
freelance84 Posted July 29, 2010 Share Posted July 29, 2010 so you're storing all your words in one SESSION variable? Why not just use two? Quote Link to comment https://forums.phpfreaks.com/topic/209214-session-interrupting-other-input-form/#findComment-1092526 Share on other sites More sharing options...
PravinS Posted July 29, 2010 Share Posted July 29, 2010 Use session_unregister() instead of session_destroy(). As session_destroy() will destroy all your sessions, while session_unregister() will only destroy the mentioned variable from session. Quote Link to comment https://forums.phpfreaks.com/topic/209214-session-interrupting-other-input-form/#findComment-1092528 Share on other sites More sharing options...
fishbaitfood Posted July 29, 2010 Author Share Posted July 29, 2010 Thanks both, And yes I'm trying to use Two Sessions, but If I have already one at the top, I get an error while using a second one somewhere else. If I use session_unregister(); ,, I can clear the array, but then I get a Warning (Wrong parameter count for session_unregister()). When I use session_unregister($_SESSION['words']); ,, I get a Notice (Undefined index: words). Edit: this is maybe because I've used unset already? No? I'm gonna trial and error a bit.. So the only way I can solve this, is to use Two Sessions? But If I put another session_start() I get a warning... UPDATE: Ok, I got session_unregister() working now: elseif (isset($_POST['reset'])) { $_SESSION['words'] = ''; session_unregister($_SESSION['words']); } But this doesn't solve my problem, listed in my first post! Quote Link to comment https://forums.phpfreaks.com/topic/209214-session-interrupting-other-input-form/#findComment-1092530 Share on other sites More sharing options...
freelance84 Posted July 29, 2010 Share Posted July 29, 2010 I don't think you've got php sessions, have a look at the manual: http://www.php.net/manual/en/function.session-start.php session_start(); $_SESSION['words_one'] = $array_words_one $_SESSION['words_two'] = $array_words_two Quote Link to comment https://forums.phpfreaks.com/topic/209214-session-interrupting-other-input-form/#findComment-1092537 Share on other sites More sharing options...
fishbaitfood Posted July 29, 2010 Author Share Posted July 29, 2010 Look, here's my whole index.php (the two last forms is what my problem is about) : <?php session_start(); header('Content-type: text/html; charset=UTF-8'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>php date + javascript time + google search</title> <script type="text/javascript"> <!-- function startTime() { var today = new Date(); var h = today.getHours(); var m = today.getMinutes(); var s = today.getSeconds(); var a; m = checkTime(m); s = checkTime(s); if(h > 11){ a = "PM"; } else { a = "AM"; } if(h < 10){ h = "0" + h; } document.getElementById('time').innerHTML = " " + h + ":" + m + ":" + s + " " + a; t = setTimeout('startTime()', 500); } function checkTime(i) { if (i<10) { // add a zero in front of numbers < 10 i="0" + i; } return i; } //--> </script> <style type="text/css"> <!-- * { margin: 0px auto; padding: 0px; } body { margin: auto; font-family: Tahoma, sans-serif; color: #098; } div#container { margin: auto; margin-top: 5px; padding: 10px; width: 40%; border: 1px solid #000; } div#container:after { content: "."; display: block; clear: both; height: 0px; visibility: hidden; } p.phptext { float: left; border: none; } span.letter { letter-spacing: 1px; } div#time { float: left; clear: right; } p.google { float: left; clear: both; margin-top: 10px; border: none; } input.text { margin-top: 5px; margin-bottom: 5px; } span.bold {font-weight: bold; letter-spacing: 1px;} span.blue {color: #4671E4;} span.red {color: #E42232;} span.yellow {color: #FFBB1C;} span.green {color: #019311;} form#searchForm { float: left; clear: right; margin-top: 5px; } input.text { width: 248px; } span.fsize { font-size: 10.75pt; } form#login { float: left; clear: both; margin: 10px 10px 0 0; } table#table { font-size: 10.75pt; color: #444; } h2.HelloTitle { color: #098; } input.lbl { margin: 5px 0 5px 0; } input.btn { font-family: Tahoma, "MS Sans Serif", Geneva, sans-serif; font-size: 9.25pt; width: 75px; height: 23px; margin: 5px 0 10px 0; } div#arrays { float: left; clear: both; margin-top: 20px; } form#arrayForm { margin: 10px 0 0 0; } div.hr { float: left; clear: both; margin: 20px 0 10px 0; width: 100%; } --> </style> </head> <body onload="startTime();"> <div id="container"> <p class="phptext">Today we are <span class="letter"> <?php setlocale(LC_ALL, 'en_gbr', 'english_england'); $weekday = (string) strftime("%A"); $day = (int) strftime("%d"); $month = (string) strftime("%B"); $year = (int) strftime("%Y"); $a = (string) ""; if ($day==1){ $a = "st of "; } elseif ($day==2){ $a = "nd of "; } elseif ($day==3){ $a = "rd of "; } else $a = "th of "; echo $weekday . ", " . $day . $a . $month . " " . $year . "."; ?> </span> </p> <div id="time"></div> <div class="hr"><hr /></div> <script type="text/javascript"> function btnText() { var box = document.getElementById('nlBox'); var btnText = document.getElementById('btnSearch'); var form = document.getElementById('searchForm'); var div = document.getElementById('divHidden'); var txt = document.getElementById('search'); if (box.checked) { btnText.value = "zoeken"; form.action = "http://www.google.nl/search"; div.innerHTML = '<input id="hidden1" name="hl" type="hidden" value="nl" /><input id="hidden2" name="lr" type="hidden" value="lang_nl" />'; txt.innerHTML = "Zoek"; } else { btnText.value = "search"; form.action = "http://www.google.com/search"; txt.innerHTML = "Search"; } } </script> <form id="searchForm" action="http://www.google.com/search" method="get"> <p class="google"><span id="search">Search</span> <span class="bold"><span class="blue">G</span><span class="red">o</span><span class="yellow">o</span><span class="blue">g</span><span class="green">l</span><span class="red">e</span></span>:<br/> <input class="text" name="q" type="text" /><br /> <input id="nlBox" name="nlBox" class="nlBox" type="checkbox" onclick="btnText();" /> <span class="fsize">Only show Dutch results</span><br /> <div id="divHidden"></div> <input id="btnSearch" class="btn" type="submit" value="search" onclick="btnText();" /> </p> </form> <div class="hr"><hr /></div> <?php if(isset($_POST['name'])){ $name = $_POST['name']; $name = strip_tags($name); $name = trim($name); $name = htmlentities($name); if (sanitize($name)) { $_SESSION['name'][] = $name; } } else { $name = ""; } ?> <form id="login" action="index.php" method="post"> <table id="table"> <tr><td><h2 class="HelloTitle">Hello <?php echo $name; ?></h2></td></tr> <tr><td> </td></tr> <tr><td><label for="name">Name:</label><br /><br /> <input id="name" name="name" type="text" value="<?php echo $name; ?>" maxlength="34" /></td></tr> <tr><td><input class="btn" type="submit" value="submit" /></td></tr> </table> </form> <div class="hr"><hr /></div> <div id="arrays"> <p>Add a word to an array below:</p> <form id="arrayForm" method="post" action="index.php"> <input type="text" name="word" maxlength="24" /> <input class="btn" type="submit" name="add" value="add" /> <input class="btn" type="submit" name="reset" value="clear array" /> </form> <p> <?php function sanitize($input) { if (!empty($input)) { $input = strip_tags($input); $input = htmlentities($input); return trim($input); } else { return FALSE; } } if (isset($_POST['add'])) { $word = $_POST['word']; $word = strip_tags($word); $word = trim($word); $word = htmlentities($word); if (sanitize($word)) { $_SESSION['words'][] = $word; for($i=0; $i < count($_SESSION['words']); $i++){ echo $_SESSION['words'][$i] . " "; } } else { echo 'Please enter a word'; } } elseif (isset($_POST['reset'])) { $_SESSION['words'] = ''; session_unregister($_SESSION['words']); } ?> </p> </div> <div class="hr"><hr /></div> </div> </body> </html> With this, I haven't got any errors, warnings, notices or whatsoever. BUT the Two forms can't operate saperately on their own.. If I edit First form, the Second disappears, also the other way arround. Quote Link to comment https://forums.phpfreaks.com/topic/209214-session-interrupting-other-input-form/#findComment-1092551 Share on other sites More sharing options...
fishbaitfood Posted July 29, 2010 Author Share Posted July 29, 2010 I used two sessions: $_SESSION['name'] and $_SESSION['words'] How can I properly fix my issue? The sessions are affecting each other, which they aren't supposed to. Quote Link to comment https://forums.phpfreaks.com/topic/209214-session-interrupting-other-input-form/#findComment-1092586 Share on other sites More sharing options...
Alex Posted July 29, 2010 Share Posted July 29, 2010 Use session_unregister() instead of session_destroy(). As session_destroy() will destroy all your sessions, while session_unregister() will only destroy the mentioned variable from session. You should not use session_unregister because it's depreciated. Instead you should just use unset to unset the session variable. unset($_SESSION['var']); Quote Link to comment https://forums.phpfreaks.com/topic/209214-session-interrupting-other-input-form/#findComment-1092588 Share on other sites More sharing options...
fishbaitfood Posted July 29, 2010 Author Share Posted July 29, 2010 Use session_unregister() instead of session_destroy(). As session_destroy() will destroy all your sessions, while session_unregister() will only destroy the mentioned variable from session. You should not use session_unregister because it's depreciated. Instead you should just use unset to unset the session variable. unset($_SESSION['var']); Ok, thanks for the tip. But still my forms won't work separately... It seems like the two 'submit' buttons are working against each other.. Although I used for one the name 'submit', and for the other one the name 'add'. So I don't know were they are encountering each other?! Quote Link to comment https://forums.phpfreaks.com/topic/209214-session-interrupting-other-input-form/#findComment-1092596 Share on other sites More sharing options...
fishbaitfood Posted July 30, 2010 Author Share Posted July 30, 2010 Could someone please explain why one form "resets" (to not display), when I submit the other form? Quote Link to comment https://forums.phpfreaks.com/topic/209214-session-interrupting-other-input-form/#findComment-1093015 Share on other sites More sharing options...
fishbaitfood Posted August 1, 2010 Author Share Posted August 1, 2010 bump Quote Link to comment https://forums.phpfreaks.com/topic/209214-session-interrupting-other-input-form/#findComment-1093813 Share on other sites More sharing options...
kenrbnsn Posted August 1, 2010 Share Posted August 1, 2010 You're using the local value when either form is submitted, so it looks like the data from the other form is wiped out. Remove most of the PHP code from after the forms and put the following at the top of your script (after the header() line): <?php function sanitize($input) { if (!empty($input)) { $input = strip_tags($input); $input = htmlentities($input); return trim($input); } else { return FALSE; } } $error = ''; if (isset($_POST['add'])) { $word = sanitize($_POST['word']); if ($word) { $_SESSION['words'][] = $word; } else { $error = 'Please enter a word'; } } if (isset($_POST['reset'])) { $_SESSION['words'] = ''; unset($_SESSION['words']); } if(isset($_POST['name'])){ $name = sanitize($_POST['name']); if (sanitize($name)) { $_SESSION['name'] = $name; } } ?> Change the login form to be: <form id="login" action="" method="post"> <table id="table"> <tr><td><h2 class="HelloTitle">Hello <?php echo $_SESSION['name']; ?></h2></td></tr> <tr><td> </td></tr> <tr><td><label for="name">Name:</label><br /><br /> <input id="name" name="name" type="text" maxlength="34" /></td></tr> <tr><td><input class="btn" type="submit" value="submit" /></td></tr> </table> </form> And the next form to be <form id="arrayForm" method="post" action=""> <input type="text" name="word" maxlength="24" /> <input class="btn" type="submit" name="add" value="add" /> <input class="btn" type="submit" name="reset" value="clear array" /> </form> <p> <?php if ($error != '') { echo $error; } if (isset($_SESSION['words'])) { echo implode(' ',$_SESSION['words']); } ?> </p> Ken Quote Link to comment https://forums.phpfreaks.com/topic/209214-session-interrupting-other-input-form/#findComment-1093820 Share on other sites More sharing options...
fishbaitfood Posted August 1, 2010 Author Share Posted August 1, 2010 Thanks for your support, Ken! I'll try it as soon as I installed EasyPHP again, because I had some installation issues with phpmyadmin.. I'll let you know asap. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/209214-session-interrupting-other-input-form/#findComment-1093848 Share on other sites More sharing options...
fishbaitfood Posted August 1, 2010 Author Share Posted August 1, 2010 Ken, thank you soo much! It works like a charm now! Quote Link to comment https://forums.phpfreaks.com/topic/209214-session-interrupting-other-input-form/#findComment-1093876 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.