Add to a dropdown box
Moderator: General Moderators
Add to a dropdown box
Lets say, i have a simple blank page, with a textbox/submit button/drop down box.. how would i go about, adding text to that drop down box.. for instance, the dropdown box starts out blank (duh :p ) and i type Blah in the textbox, and click the submit button, how can i get Blah in that dropdown box? ;o
The easiest way to do that in my opinion would be to store all the values that you wanted in the dropdown box in a external data source (such as a mySQL table, flat file) and then go through each one of the values in the external data source and print out the neccesary HTML to make a dropdown box with those options.
Example - Data is stored in mySQL database:
Then just make another script that will add values to the table that holds all the values that go in the select box.
Did this help?
Example - Data is stored in mySQL database:
Code: Select all
<?php
define(HOST, "hostname");
define(USER, "username");
define(PASS, "password");
define(DBASE, "database");
define(TABLE, "table");
mysql_connect(HOST,USER,PASS) or die ("Failed to establish connection with dbase.");
mysql_select_db(DBASE);
$result = mysql_query("select value from " . TABLE);
if ($result)
{
print '<select name="selectBox" multiple>';
while ($row = mysql_fetch_array($result))
{
print "<option>$rowї0]</option>";
}
print '</select>';
}
?>Did this help?
- trollll
- Forum Contributor
- Posts: 181
- Joined: Tue Jun 10, 2003 11:56 pm
- Location: Round Rock, TX
- Contact:
Haven't played around enough with these examples to know if they'd work across the major platforms and browsers, but they should at least point you in the right direction...
Last edited by trollll on Sat Aug 16, 2003 11:25 pm, edited 1 time in total.
- trollll
- Forum Contributor
- Posts: 181
- Joined: Tue Jun 10, 2003 11:56 pm
- Location: Round Rock, TX
- Contact:
Wow, good timing! Nigma, my new rival! 
But I don't think you quite answered the question, unless the admin will edit the select and users won't (or something like that). I've actually come across projects where the user needed to type in something and pass it from the text field to a select list and have it transfered just on the front-end.
But I don't think you quite answered the question, unless the admin will edit the select and users won't (or something like that). I've actually come across projects where the user needed to type in something and pass it from the text field to a select list and have it transfered just on the front-end.
It probably wasn't neccesary to use the defines here, but in big coding projects you should always define stuff like database & table names so that when you want to use a new dbase or table you don't have to go digging through your code just to change the dbase name because you defined all those at the top.
You follow?
You follow?
Trolll, why didn't that answer the question? You could do something like this:
Make sense ? Please inform me if my logic is "whack" 
Code: Select all
<?php
include("pathToScriptThatGetsValuesAndPrintsSelectBox");
/* Put the code that will allow users to add values to the value database
When user submits their new value it sends them to another script that adds the value and prints "Value Added" if it was added succesfully
and then sends them back to this page */
?>Okay, well heres what i came up with. Theres probably better ways, but..yea.. Doesnt matter what you name this file, and you dont have to make stuff.txt, it makes itself:
The first time you run it, if it says the file you're trying to chmod doesnt exsist, just refresh it, that sets the permissions on the file, you might not even need it.
Code: Select all
<?php
echo "<form method=get action=$PHP_SELF><input type=text name=text><input type=submit></form><br>";
if($text){
$stuff = "<option>$text";
chmod ("stuff.txt", 0755);
$out = fopen("stuff.txt", "a+");
fwrite($out, $stuff);
fclose($out);
}
echo "<select name=drop>";
include("stuff.txt");
echo "</select>";
?>The first time you run it, if it says the file you're trying to chmod doesnt exsist, just refresh it, that sets the permissions on the file, you might not even need it.
Muhaha! Your didn't work for me, so here is what I have.
Usually I don't do full things like this, but I am all excited 
Code: Select all
<?php
$valueToAdd = $_POSTї'vta'];
if (!empty($valueToAdd))
{
$fp = fopen("values.txt", "a");
fputs($fp, "<option>$valueToAdd</option>");
fclose($fp);
}
else { } // Do nothing because they didn't enter any valid input
?>
<html>
<head><title>A Page</title></head>
<body>
<form method="post" action="<?php print $PHP_SELF ?>">
<input type="text" name="vta"><input type="submit">
</form>
<br><br>
<select multiple>
<?php
if (file_exists("values.txt"))
{
$fp = fopen("values.txt", "a+");
while (!feof($fp))
{
$value = fgets($fp, 1024);
print "$value<br>";
}
}
?>
</select>
</body>
</html>js
To enter into the fray if you want the values permetely stored then you would have to use a db or flat file.
If you want to just put the value in the drop down list use a little javascript and presto it will appear without having to return to the host.
Of cousre this is only temporary and will be lost when the page gets submitted.
phpScott
If you want to just put the value in the drop down list use a little javascript and presto it will appear without having to return to the host.
Of cousre this is only temporary and will be lost when the page gets submitted.
phpScott