CG_dude Posted February 25, 2009 Share Posted February 25, 2009 Hi all, I appreciate any help on this issue. I'm may need a if and else statement, hoping not, since I'm not great with those, but here is the isssue. I have a drop down list that has 2 options, if one is selected, then I need the selection to stay selected until I come back and change it. Right now, all is working great except everytime the page refreshes, it goes back to default. Is there a way to do this with a text file? Here is the code I have <form name="myform"> <select name="mylist" onchange="disp_text()"> <option value="Made SLA">Made SLA</option> <option value="Missed SLA">Missed SLA</option> </select> <P><input type="submit" name="submit" value="sla"/> </form> Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 25, 2009 Share Posted February 25, 2009 Well, what do you mean by "until I come back and change it"? What are you doing with the value when you submit the page? You should be using/saving it somewhere. Based upon where you are saving that valule the solutino would be slightly different. Here's an example assuming the value is saved to a session value: $options = array('Made SLA', 'Missed SLA'); echo "<select name=\"mylist\" onchange=\"disp_text();\">"; foreach($options as $value) { $selected = ($_SESSION['mylist']==$value)?' selected="selected" : ''; echo " <option value=\"$value\"{$selected}>{$value}</option>\n"; } echo "</select>\n"; Quote Link to comment Share on other sites More sharing options...
CG_dude Posted February 25, 2009 Author Share Posted February 25, 2009 Thanks for the help, however, it's having issues with this line $selected = ($_SESSION['mylist']==$value)?' selected="selected" : '; Not familiar with session. also, when I say "until I come back and change it" I mean when I come back to my website and physically change it. As for saving it, I don't have any place I'm saving the out put to, that was part of my question. I just need to be able to select the option, have the page retain it, until I change it by selecting the other option. I'm not sure how to code what I am needing. Thanks again for your help, I'm open to suggestions, maybe a check box is better? Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 25, 2009 Share Posted February 25, 2009 The line I posted is corrected below. As for how/where you are saving it, I guess I'm confused as to what the pupose it. If you are not using the value for anything then what is the point of saving the value? You need to save the value somewhere. If you want it saved for when you come back to the site, then sessions will not work. Does the value need to be saved for "everyone" or will the saved value be different for different people. A second solution would be to use a cookie, but that value would be different for each user/pc that accesses the page. Otherwise you would need to save to a database or to a flat file. But, since the value isn't used for anything I don't see the point. $selected = ($_SESSION['mylist']==$value)?' selected="selected"' : ''; Quote Link to comment Share on other sites More sharing options...
CG_dude Posted February 25, 2009 Author Share Posted February 25, 2009 I guess that is my point, how do you save your choice to a "flat file" so when I select it, all who view the page will see it the selection. I am reporting on a end time of a job run. each night, if that job misses it's sla time, I select missed sla until the next night where it will likely makes it's sla. Hope this helps, I know I need to write to a file just not sure how to do it in php and using a drop down list, and it needs to be a text file. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 25, 2009 Share Posted February 25, 2009 <?php //Force browser to no cache the page header("Cache-Control: no-cache, must-revalidate"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); //Set some vars $slaStatus = ""; $slaStatusID = 0; $statusFile = "slaStatus.txt"; $options = array('Missed SLA', 'Made SLA'); //Check if new status was posted if (isset($_POST['slaStatus'])) { //Set the status id and status string from post $slaStatusID = $_POST['slaStatus']; $slaStatus = "Status: " . $options[$slaStatusID]; //Write status id to file $fh = fopen($statusFile, 'w') or die("can't open file"); fwrite($fh, $slaStatusID); fclose($fh); } else if (is_file($statusFile)) { //Set the status id and status string from file $slaStatusID = file_get_contents($statusFile); $slaStatus = "Status: " . $options[$slaStatusID]; } else { $slaStatus = "SLA Status has not been set"; } ?> <html> <body> <div><?php echo $slaStatus; ?></div> <form name="slaForm" action="" method="POST"> <select name="slaStatus" onchange="disp_text();"> <?php foreach($options as $id => $value) { $selected = ($slaStatusID==$id)?' selected="selected"' : ''; echo " <option value=\"$id\"{$selected}>{$value}</option>\n"; } ?> </select> <br /> <button type="submit">Change SLA Status</button> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
CG_dude Posted February 26, 2009 Author Share Posted February 26, 2009 Thanks MJDAMATO, with a few modifications of your post, all is working great, thanks, here is the code I'm using, I removed file_get_contents and replaced it with include and I removed the top 2 header lines. Thanks again for the support! <?php //Set some vars $slaStatus = ""; $slaStatusID = 0; $statusFile = "/users/apache/hpws/apache/htdocs/prod_report/slaStatus.txt"; $options = array('Missed SLA', 'Made SLA'); //Check if new status was posted if (isset($_POST['slaStatus'])) { //Set the status id and status string from post $slaStatusID = $_POST['slaStatus']; $slaStatus = "Status: " . $options[$slaStatusID]; //Write status id to file $fh = fopen($statusFile, 'w') or die("can't open file"); fwrite($fh, $slaStatusID); fclose($fh); } else if (is_file($statusFile)) { //Set the status id and status string from file $slaStatusID = include($statusFile); $slaStatus = "Status: " . $options[$slaStatusID]; } else { $slaStatus = "SLA Status has not been set"; } ?> <html> <body> <div><?php echo $slaStatus; ?></div> <form name="slaForm" action="" method="POST"> <select name="slaStatus" onchange="disp_text();"> <?php foreach($options as $id => $value) { $selected = ($slaStatusID==$id)?' selected="selected"' : ''; echo " <option value=\"$id\"{$selected}>{$value}</option>\n"; } ?> </select> <br /> <button type="submit">Change SLA Status</button> </form> </body> </html> Quote Link to comment 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.