Add to a dropdown box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NicEJoB
Forum Newbie
Posts: 14
Joined: Tue Jul 15, 2003 8:31 pm

Add to a dropdown box

Post 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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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)
&#123;
  print '<select name="selectBox" multiple>';
  while ($row = mysql_fetch_array($result))
  &#123;
    print "<option>$row&#1111;0]</option>";
  &#125;
  print '</select>';
&#125;
?>
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?
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post 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...
Last edited by trollll on Sat Aug 16, 2003 11:25 pm, edited 1 time in total.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Muhaha! I beat you to it Trolll!!
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post 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.
NicEJoB
Forum Newbie
Posts: 14
Joined: Tue Jul 15, 2003 8:31 pm

Post 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);
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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" ;)
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post 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.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Muhaha! Your didn't work for me, so here is what I have.

Code: Select all

<?php
$valueToAdd = $_POST&#1111;'vta'];

if (!empty($valueToAdd))
&#123;
  $fp = fopen("values.txt", "a");
  fputs($fp, "<option>$valueToAdd</option>");
  fclose($fp);
&#125;
else &#123; &#125; // 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"))
&#123;
  $fp = fopen("values.txt", "a+");
  while (!feof($fp))
  &#123;
    $value = fgets($fp, 1024);
    print "$value<br>";
  &#125;
&#125;
?>
</select>
</body>
</html>
Usually I don't do full things like this, but I am all excited ;)
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

js

Post 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
Post Reply