Jump to content

Add entries to array


eliZZZa

Recommended Posts

Don´t know, if the title is correct for my request.

What I want to achieve:

A select list in a form should grow with every data entry.

Example:

I have a list of towns to choose from for a single entry.

If there is a town, not in the list, I would like to have the option to enter one in a text field.

Next time I approch the input form, the newly entered town shows up in the select list.

 

What do I have to do, to expand the select list (resp. the table behind)?

 

Thanks in advance and kind regards from Austria

eliZZZa

Link to comment
Share on other sites

Im assuming you want to use MYSQL to handle this unless your a fan of flat file

 

Its pretty simple

 

Query the table of towns to see if the town the user has imputted is already stored in there

 

If its not, insert it

 

You will need to populate your <SELECT> direct from a mysql query aswell, it obviously cant be static

 

Thats how its done, can translate that into code, if not perhaps post a snippet of your html and let one of us turn it into some working dynamic code :)

Link to comment
Share on other sites

If its going to be used heavily it would be better to design a nice database

 

Otherwise, a little flat file wouldnt go a miss using explode to split up the towns and throw them into an array ready to be looped out into html and your form :D

Link to comment
Share on other sites

There's no error checking or database handling here, and I haven't tested the code, but it should work:

 

SQL: (State is obviously optional)

Create Table Location(
      LocID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
      City VARCHAR(100),
      State VARCHAR(2)
);

 

PHP for building the options:

(note there isn't any error checking here, and I haven't tested this out, so I'm not sure if it works)

$result = mysql_query("SELECT City FROM Location WHERE STATE='". $State . "'");
echo "<select name='city'>";
while ($record = mysql_fetch_row($result, MYSQL_ASSOC)) {
    $city = $record['City'];
    echo "<option value='$city'>$city</option>";
}
echo "</select>\n";

 

PHP for building adding the new city to the database:

$city = $_POST['City']; // or whatever the name of the text field in the HTML is
$State = ""; // Not sure how you want to handle this.
$result = mysql_query("INSERT INTO Location SET City='". $city . "', State='" . $State ."'");

 

 

Link to comment
Share on other sites

WOW - I´m overwhelmed by all of your replies! Many thanks, I am right now going, if I get it to work.

Everything runs out of a huge MySQL database anyway, I even managed to set up lookup tables and to query them (I have no idea about PHP, but I am good in copy-paste+logic >;o))

 

I will give feedback, if I succeeded >;o)

eliZZZa

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.