-1

I'm not sure if this is even possible and my research is not resulting in much. I have a program I'm giving to a client with an admin page. I need him to be able to add new inspectors to the html drop down selector if someone is hired. I'm able to append it as you see below but is it some way possible to make these changes permanent without having him make physical changes to the code?

The form (p tag just for testing and I'd like the new appended option tag to stick)

HTML

<p>Add inspector</p>
<select id="mySelect" name="inspector">
    <option value=1>Ramond Valez</option>
    <option value=2>Daniel Rivera</option>
    <option value=5>Jimmy Smith</option>
    <option value=6>Jasmine Green</option>
</select>

PHP

$id = 79;//these are just for testing. Real database inputs will replace theses
$name = "Dylan";

jQuery

$(document).ready(function(){
    $("p").click(function(){
      $('#mySelect').append("<option value='<?Php echo $id; ?>'><?php echo $name; ?></option>")

    });
});
Dorado
  • 411
  • 2
  • 15
Dylano236
  • 305
  • 3
  • 15
  • why do it with jquery - why not just load them server side? – Pete May 02 '18 at 14:50
  • Your values need to be stored in a database and then retrieved and bound to the dropdown. – Turnip May 02 '18 at 14:50
  • @turnip yes I know, those are just for testing as my comments says. I'll retrieve these from the database when a new user is added. My problem is I want this appended option tag to stick after it's appended – Dylano236 May 02 '18 at 14:53
  • @pete Thanks, can you go into a bit more detail about what you mean? My issue is when the guy adds a new inspector I want the new inspectors ID and Name added to my drop down form – Dylano236 May 02 '18 at 14:55
  • My point was that the _new_ value needs to be stored in the database too. – Turnip May 02 '18 at 14:56
  • https://stackoverflow.com/questions/5189662/populate-a-drop-down-box-from-a-mysql-table-in-php – Pete May 02 '18 at 14:57
  • @pete thanks, I understand now. If I was able to give you a point I would. Appreciate the help. – Dylano236 May 02 '18 at 15:00
  • @Turnip thanks, I did not think of using the database but makes perfect sense. – Dylano236 May 02 '18 at 15:01

2 Answers2

1

If you have a database you can read input values from then you can store drop down values in the database as well. Create a table with your drop down values, get the values from the database and then iterate through the table to make the drop down list. A database is the way to store data from session to session.

Skeik
  • 51
  • 1
  • 4
  • Great. I did not think of that but makes sense. I can add options to the database then just loop through which are relevant at the time. I appreciate the help! – Dylano236 May 02 '18 at 14:58
1

First of all you need to store the data how @Skeik say's..

So the data flow...

Steps:

  • Input element inside a form or Ajax call to the server side.
  • Retrieve the new "Inspector name"
  • Then with that name you should have a connection of your database ready for make an insert.

With ajax call to server side:

  • Retrieve the ID after insert the new "Inspector" and with your jQuery code, add this one instantly.

Without ajax call:

  • When the "user" add this one, gonna make a "POST" call, so the page gonna refresh, you should iterate over them how @Skeik say's to you.