Jump to content

Drop Down Box - User Editable


Luvz2Fly

Recommended Posts

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!

 

 

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.