/    Sign up×
Community /Pin to ProfileBookmark

editable dropdown box?

is it possible to have an editable dropdown list in PHP? how? plz help
e.g. I have a product list, but some new products are not added into this list yet, so when user comes to this point, he/she can type in the product name instead of having to select on from the existing list.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@purefanMay 01.2006 — you can have an input field in plain html, then form it to a php file that adds it to file (database or plain text file, whatever you prefer), and when showing the page you take the drop down list elements from the file
Copy linkTweet thisAlerts:
@ssffccauthorMay 01.2006 — thx for the quick help, but I am new to PHP, could u plz be more specific in code?
Copy linkTweet thisAlerts:
@purefanMay 02.2006 — ok ?

You need 2 pages, one for displaying and one for storing,

1.The displaying page:
[code=php]
#Suppose you are taking your options from a flat file
#called options.txt

#Step 1: Read the contents of the file
$datalines = file ("options.txt");

echo "<form action="store.php" method="post">";
echo "<select name="MyName">";
foreach ($datalines as $zz) {
echo "<option value="".$zz."">".$zz . "</option>";
}
echo "</select>";
#That should display each new line in the text file as an
#option in the drop down menu.


#Lets now allow the input of an entry:
echo "<input type="text" name="newone"><input type="submit">";
#and close the form
echo "</form>";
[/code]


2.Now the store.php file which will store the text from newone in the options.txt file:
[code=php]
//store.php
#Lets check if the user did input a text:
if(empty($_POST["newone"])){
//the input field is empty so he chose from the drop down menu
//Fill in here with whatever you need
}
else {
//It is not empty so we append it:
$myFile = "options.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_POST["newone"]."n";
fwrite($fh, $stringData);
fclose($fh);
#Now the new item is in the flat file
#I think you need to do something with the $_POST["newone"] besides
#from adding it so just use it for whatever you want
}
[/code]


Im sure ppl here might have much better solutions but I think this should work
×

Success!

Help @ssffcc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...