Darkmatter5 Posted January 7, 2009 Share Posted January 7, 2009 I have four checkboxes that toggle on and off different divs in the page. The checkboxes and javascript code works great, but when you click on a button in the forms and some new php code is run and creates a box displaying the queries status, the page gets reset and the check boxes all get put to their original state and the div closes back up. How can I make it so that when the button is pressed and the status box is written the state of the check boxes and the state of the div is retained? Here's the code I think you need. Notice the "add_system" and "search" buttons. those are the buttons that produce the status box. <script type="text/javascript"> function toggle(thediv) { var el=document.getElementById(thediv); if(el.style.display!='none') { el.style.display='none'; } else { el.style.display=''; } } </script> <body> <input type="checkbox" name="toggledivs" onclick="toggle('site_admin');" />Site Administration <input type="checkbox" name="toggledivs" onclick="toggle('user_admin');" />User Administration <input type="checkbox" name="toggledivs" onclick="toggle('news_man');" />News Management <input type="checkbox" name="toggledivs" onclick="toggle('data_man');" />Data Management <div id="site_admin" style="display:none;">test</div> <div id="user_admin" style="display:none;">test</div> <div id="news_man" style="display:none;">test</div> <div id="data_man" style="display:none;"> <fieldset style="background-color: #000000"> <legend><b>SYSTEMS</b></legend> <form method="post" enctype="multipart/form-data"> <table width="300" border="0"> <tr><td><b>Add new system</b></td></tr> <tr><td> <div> <label for="name">System name:</label><br><input type="text" name="name" id="name" size="50" maxlength="50"/><br> <label for="file">Filename:</label><br><input type="file" name="file" id="file" size="50" maxlength="50"/><br> <input name='add_system' type='submit' class='button' tabindex='3' value='Add new system'/> </div> </td></tr> </table> </form> <hr> <form method="post"> <table width="300" border="0"> <tr><td><b>Edit a system</b></td></tr> <tr><td> <div> <label for="systems">Select a system</label><br><?php $vein->list_systems(1,$_GET['systems']); ?><br> <input name='search' type='submit' class='button' tabindex='3' value='Search now'/> </div> </td></tr> </table> </form> </fieldset> <hr> <fieldset style="background-color: #000000"> <legend><b>PUBLISHERS</b></legend> </fieldset> </div> <?php if(isset($_POST['add_system'])) { echo "<div class='info_wrapper'> <div class='info_top-left'> </div><div class='info_top'> </div><div class='info_top-right'> </div> <div class='info_left'> </div> <div class='info_content'> <table width='290' align='center' border='0'> <tr><th>Submission Status</th></tr> <tr><td><ul>"; if($_POST['name']=="") { echo "<li><span class='dbstatusRED_text'>System name was left blank!</span></li></ul></td></tr>"; } else { include 'library/config.inc.php'; $conn=mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql'); mysql_select_db($dbname); $countquery=mysql_query("SELECT COUNT(name) as count FROM systems WHERE name='$_POST[name]'") or die(mysql_error()); $count=mysql_fetch_array($countquery); if ($count['count']>0) { echo "<li><span class='dbstatusRED_text'>'$_POST[name]' already exists in the database!</span></li></ul></td></tr>"; } else { if($_FILES["file"]["name"]!="") { if($_FILES['file']['error']>0) { echo "<li><span class='dbstatusRED_text'>Error: " .$_FILES['file']['error']. "</span></li>"; } else { if(file_exists("images/" .$_FILES['file']['name'])) { echo "<li><span class='dbstatusRED_text'>'" .$_FILES['file']['name']. "' already exists!</span></li>"; } else { //move_uploaded_file($_FILES['file']['tmp_name'],"images/" .$_FILES['file']['name']); echo "<li><span class='dbstatusGREEN_text'>'" .$_FILES['file']['name']. "' stored!</li>"; } } } //$insertquery=mysql_query("INSERT INTO systems (name, sys_logo, manufacturer, man_logo) VALUES ('$_POST[name]','$_FILES[file][name]','')") or die(mysql_error()); echo "<li><span class='dbstatusGREEN_text'>'$_POST[name]' added to the database!</span></li></ul></td></tr>"; } mysql_close($conn); } echo "</table> </div> <div class='info_right'> </div> <div class='info_bot-left'> </div><div class='info_bot'> </div><div class='info_bot-right'> </div> </div>"; } ?> Link to comment https://forums.phpfreaks.com/topic/139898-help-retaining-status-of-form-when-new-data-is-generated/ Share on other sites More sharing options...
chronister Posted January 8, 2009 Share Posted January 8, 2009 You can use a cookie or a session var to store the state of the open divs. I am not real familiar with cookies, but you can use a session var like this... <?php session_start(); $_SESSION['openDiv'] = 'id of open div'; ?> Then you would need some code to look at the session var set above, and compare it with what is being written on the page. If they match, then that div should have a style of block or something similar, if it does not match, then style = display:none... That is the rough idea. Hope it helps. Nate Link to comment https://forums.phpfreaks.com/topic/139898-help-retaining-status-of-form-when-new-data-is-generated/#findComment-732119 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.