eliZZZa Posted July 21, 2007 Share Posted July 21, 2007 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 Quote Link to comment Share on other sites More sharing options...
jaymc Posted July 21, 2007 Share Posted July 21, 2007 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 Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 21, 2007 Share Posted July 21, 2007 Do you want this new town to show up for EVERY user, or just the user that added it? If you want it for every user, then I would go with what jaymc said, use a flat-file or a database. Quote Link to comment Share on other sites More sharing options...
jaymc Posted July 21, 2007 Share Posted July 21, 2007 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 Quote Link to comment Share on other sites More sharing options...
socratesone Posted July 21, 2007 Share Posted July 21, 2007 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 ."'"); Quote Link to comment Share on other sites More sharing options...
socratesone Posted July 21, 2007 Share Posted July 21, 2007 A couple things I just noticed: #1: You'll want to tell the difference between the text field city and the select city, so <select name="citySelect">...etc</select> and <input type="test" name="cityText"> ...would be better Quote Link to comment Share on other sites More sharing options...
eliZZZa Posted July 21, 2007 Author Share Posted July 21, 2007 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 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.