Luvz2Fly Posted February 23, 2008 Share Posted February 23, 2008 Hi folks! Brand spanking new to PHP so I apologize in advance for this easy one ;-) without using a DB what is the best way to make drop down box selection (10-20 items) editable via a form (Add/Delete items only). I assume the array data would be stored in some sort of flat file? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted February 23, 2008 Share Posted February 23, 2008 Without a database you will need a file. check out http://www.tizag.com/phpT/files.php Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 23, 2008 Share Posted February 23, 2008 This really isn't a PHP question. There is no HTML control for creating an editable select list. There are some JavaScript hacks out there for doing this, but I can't attest to their cross-browser capability. I would suggest having an "<--Other-->" item in the select list with a text field where the usre can enter the "other" value. Then when the user submits the form you can take the entered value and add it to the list of possible values used to populate the list. You could either do this with a database or a flat file (the database option being more reliable). Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 23, 2008 Share Posted February 23, 2008 Here's a fully working example script: NOTE: Forum would not allow the file commands in the code, so I replaced them with '[FOPEN]', '[FCLOSE]' & '[FWRITE]' in the script below. Remove the brackets and make them lower case. <?php $file = "names.txt"; $names_list = file($file); foreach ($names_list as $key => $name) { $names_list[$key] = str_replace('\n', '', trim($name)); } if ($_POST['name_sel']!='') { $submit_name = $_POST['name_sel']; } else if ($_POST['name_other']!='') { $submit_name = $_POST['name_other']; $new_name = trim($_POST['name_other']); if (!in_array($new_name, $names_list)) { $names_list[] = $new_name; sort($names_list); //Write the new list to the file $fh = [FOPEN]($file, 'w') or die("can't open file"); foreach($names_list as $name) { [FWRITE]($fh, $name."\n"); } [FCLOSE]($fh); } } ?> <html> <head> <script type="text/javascript"> function other_name() { nameSel = document.getElementById('name_sel'); nameTxt = document.getElementById('name_other'); if (nameSel.value=='') { document.getElementById('name_other').disabled = false; document.getElementById('name_other').style.backgroundColor = '#ffffff'; } else { document.getElementById('name_other').value = ''; document.getElementById('name_other').disabled = true; document.getElementById('name_other').style.backgroundColor = '#cecece'; } } </script> </head> <body onload="other_name();"> <form name="testForm" id="testForm" onload="other_name();" method="POST"> <?php echo "Name: <select name=\"name_sel\" id=\"name_sel\" onchange=\"other_name();\">\n"; echo " <option value=\"\"><--Other--></option>\n"; foreach ($names_list as $name) { $slected = ($name==$submit_name)?' selected="selected"':''; echo " <option value=\"$name\"$slected>$name</option>\n"; } echo "</select><br><br>\n"; ?> Other: <input type="text" name="name_other" id="name_other" /><br><br> <button type="submit">Submit</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.