Page 1 of 1
Add to a dropdown box
Posted: Sat Aug 16, 2003 11:12 pm
by NicEJoB
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
Posted: Sat Aug 16, 2003 11:24 pm
by nigma
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:
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>';
}
?>
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?
Posted: Sat Aug 16, 2003 11:24 pm
by trollll
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...
Posted: Sat Aug 16, 2003 11:24 pm
by nigma
Muhaha! I beat you to it Trolll!!
Posted: Sat Aug 16, 2003 11:31 pm
by trollll
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.
Posted: Sat Aug 16, 2003 11:31 pm
by NicEJoB
yah, i guess i could use MySQL but im trying to figure it out an easier way, btw u dont need to define them for connecting to a db, works both ways
mysql_connect("hostname","user","pass")
mysql_select_db(dbname);
Posted: Sat Aug 16, 2003 11:33 pm
by nigma
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?
Posted: Sat Aug 16, 2003 11:38 pm
by nigma
Trolll, why didn't that answer the question? You could do something like this:
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 */
?>
Make sense ? Please inform me if my logic is "whack"

Posted: Sun Aug 17, 2003 12:21 am
by Drachlen
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:
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.
Posted: Sun Aug 17, 2003 1:49 am
by nigma
Muhaha! Your didn't work for me, so here is what I have.
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>
Usually I don't do full things like this, but I am all excited

js
Posted: Sun Aug 17, 2003 1:29 pm
by phpScott
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