kkiran Posted February 21, 2008 Share Posted February 21, 2008 Hi! I know how to do this in .Net using page.load method but I am clueless when it comes to PHP. The issue I am facing : I have an Array (Session). I enter new values into the Array and it should check for duplicates and then take the element if it is unique. I wrote the code and interestingly, it does a fairly decent job except that it first takes in a duplicate value and from then on it detects the duplicates. I guess it got something to do with the page load like method if any in PHP. Any help is appreciated My Code : <?php session_start(); $temp = count($_POST['mycheckbox1']); for($loop2 = 0; $loop2 < $temp; $loop2++){ $_SESSION['softwares'][] = $_POST['mycheckbox1'][$loop2]; } if($_POST['txt_software']=='') echo 'No value selected / Duplicate Software '; else { if (isset($_POST['txt_software'])) { /*foreach ($_SESSION["softwares"] as $tempVal) { if(strtolower($tempVal)==strtolower(trim($_POST["txt_software"]))) $dupSessionValuesFound=true; else $dupSessionValuesFound=false; }*/ if (!in_array(trim($_POST['txt_software']),$_SESSION["softwares"])){ //if($dupSessionValuesFound=false){ $dupArray=false; $debug = "submission"; //$duplicateSoftwareFound = false; $countSoftware = count($_SESSION['softwares']); for($i=0;$i<$countSoftware;$i++) { if(strtolower(trim($_POST["txt_software"]))==strtolower($_SESSION["softwares"][$i]['txt_software'])) $dupArray=true; } if(!$dupArray) { //$_SESSION['softwares'][$countSoftware-1]['text_software'] = $_POST['txt_software']; $_SESSION['softwares'][] = array(); $countSoftware = count($_SESSION['softwares']); $_SESSION['softwares'][$countSoftware-1]['txt_software'] = trim($_POST['txt_software']); $_SESSION['softwares'][$countSoftware-1]['text_lab'] = $_POST['txt_lab']; $_SESSION['softwares'][$countSoftware-1]['text_license'] = $_POST['txt_license']; $_SESSION['softwares'][$countSoftware-1]['text_media'] = $_POST['txt_install_media']; } else echo "<b><font color=red>Duplicate stuff detected</font></b>"; } else echo "<b><font color=red>Duplicate stuff detected</font></b>"; } } ?> <html> <head> <script type="text/javascript"> function submit_page (action) { //alert(action); switch (action) { case "done": document.form1.action = "ConfirmationPage.php"; //alert(document.form1.action); document.form1.submit(); break; case "request": document.form1.action = "AdditionalSoftware.php"; document.form1.submit(); } break; default: break; } } </script> </head> <body> <div style="bottom: 0px; right: 0px; position:absolute;"><?php print_r($_SESSION["softwares"]); echo "<p>" . $debug . "</p>"; ?> </div> <p align="center" class="style1">Below is a list of software you have stated use of:</p> <form id="form1" name="form1" method="post"> <?php for ($i=0; $i < count($_SESSION["softwares"]); $i++) { if (is_array($_SESSION["softwares"][$i])) echo $_SESSION["softwares"][$i]["txt_software"] . "<br />"; else echo $_SESSION["softwares"][$i] . "<br />"; } ?> <div align="left">If there is additional software that you use and it is not listed here, please enter it below :</div> <? if (isset($_POST['txt_software'])) { if($checkDuplicate!=false) echo "<b><font color=red>Software already in the list</font></b>"; } ?> <p> <table> <tr><td>Name of Software</td><td>Lab</td><td>Install Media</td><td>License</td></tr> <tr><td> <input name="txt_software" id="txt_software" type="text" value="" size="30" /> </td> <td> <select name="txt_lab"> <? $db = mysql_pconnect("its-dev-vm","webuser","password"); mysql_select_db("swrequest"); $query = "Select Distinct(LabName) LabName from labs Order by LabName"; $result = mysql_query($query); $num_results = mysql_num_rows($result); echo $num_results; for ($i=0; $i < $num_results; $i++){ $row = mysql_fetch_array($result); $stringValue = htmlspecialchars( stripslashes($row["LabName"])); echo "<option value=\"$stringValue\" />"; echo htmlspecialchars( stripslashes($row["LabName"])); echo "</option>"; //echo "<br />"; } ?> </select> </td> <td> <input name="txt_install_media" id="txt_install_media" type="text" value="" size="30" /> </td> <td> <input name="txt_license" id="txt_license" type="text" value="" size="30" /> </td> </tr> </table> <div align="left"> <input type="button" name="request" id="request" value="Request another Software" onClick="javascript:submit_page(this.name);" /> <input type="button" name="done" id="done" value="Done" onClick="javascript:submit_page(this.name);" /> </div> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/92309-php-sessions-duplicate-arrays-and-post-back-issues/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.