| | |
Using <select> tag with PHP
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Dec 2007
Posts: 20
Reputation:
Solved Threads: 0
i was trying to configure about how to work with <select> tag, when i was stuck with this problem: when i choose the <select> tag with PHP, i just want that that value of the <option> tag on the <select> tag will be the value selected after i submit the form. but the thing is, it will go back to the default value. Just try to look at the code:
so this is my code. I try to fetch some data from my database.
anyone can help. I really need your help guys. thanks
PHP Syntax (Toggle Plain Text)
<select name="project"> <option value="">-Select one-</option> --> <?php while($proj = mysql_fetch_array($qryS)){ $id = $proj['id']; $project = $proj['project']; $i = $i + 1; echo "<option value=\"$i\""; echo ($i == $id) ? "SELECTED":""; echo ">" . $project . "</option>"; } ?> </select>
so this is my code. I try to fetch some data from my database.
anyone can help. I really need your help guys. thanks
pLan yOur wOrK and wOrK yoUr pLan
•
•
Join Date: Dec 2008
Posts: 19
Reputation:
Solved Threads: 1
Hi,
This is very simple. I can help You. Just follow the code given below
Just remove username,password, databasename, tablename,fieldname as you have.
I hope this will work
This is very simple. I can help You. Just follow the code given below
php Syntax (Toggle Plain Text)
$con=mysql_connect("localhost","username","password"); if (!$con) die("Couldn't connect to MySQL"); mysql_select_db("databasename",$con) or die("Couldn't open admin: ".mysql_error()); $q1=mysql_query("SELECT <fieldname> FROM <tablename>"); ?> <select name="<fieldname>"> <? while($row=mysql_fetch_array($q1)) { ?> <option value="<?php echo $row['<fieldname>'];?>"> <?php echo $row['<fieldname>']; ?> </option> <? } ?> </select>
Just remove username,password, databasename, tablename,fieldname as you have.
I hope this will work
Last edited by peter_budo; Dec 23rd, 2008 at 7:05 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
•
•
Join Date: Dec 2007
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
Hi,
This is very simple. I can help You. Just follow the code given below
$con=mysql_connect("localhost","username","password");
if (!$con)
die("Couldn't connect to MySQL");
mysql_select_db("databasename",$con)
or die("Couldn't open admin: ".mysql_error());
$q1=mysql_query("SELECT <fieldname> FROM <tablename>");
?>
<select name="<fieldname>">
<?
while($row=mysql_fetch_array($q1))
{
?>
<option value="<?php echo $row['<fieldname>'];?>">
<?php
echo $row['<fieldname>'];
?>
</option>
<?
}
?>
</select>
Just remove username,password, databasename, tablename,fieldname as you have.
I hope this will work
pLan yOur wOrK and wOrK yoUr pLan
•
•
Join Date: Dec 2007
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
i was trying to configure about how to work with <select> tag, when i was stuck with this problem: when i choose the <select> tag with PHP, i just want that that value of the <option> tag on the <select> tag will be the value selected after i submit the form. but the thing is, it will go back to the default value. Just try to look at the code:PHP Syntax (Toggle Plain Text)
<select name="project"> <option value="">-Select one-</option> --> <?php while($proj = mysql_fetch_array($qryS)){ $id = $proj['id']; $project = $proj['project']; $i = $i + 1; echo "<option value=\"$i\""; echo ($i == $id) ? "SELECTED":""; echo ">" . $project . "</option>"; } ?> </select>
so this is my code. I try to fetch some data from my database.
anyone can help. I really need your help guys. thanks
pLan yOur wOrK and wOrK yoUr pLan
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
hi there..
i almost got the same problem.
i have 3 drop down menus in my page.
i was able to populate the first drop down menu.
the 2nd and 3rd drop down menu was not successfully
populated.
hope you can help me.
my query goes:
$query = "SELECT table1.col1, table2.col2
FROM table1, table2"
the first drop down menu would work, not in the succeeding
drop down menu.
Thanks,
Tintin
================================================
i almost got the same problem.
i have 3 drop down menus in my page.
i was able to populate the first drop down menu.
the 2nd and 3rd drop down menu was not successfully
populated.
hope you can help me.
my query goes:
$query = "SELECT table1.col1, table2.col2
FROM table1, table2"
the first drop down menu would work, not in the succeeding
drop down menu.
Thanks,
Tintin
================================================
•
•
•
•
Hi,
This is very simple. I can help You. Just follow the code given below
php Syntax (Toggle Plain Text)
$con=mysql_connect("localhost","username","password"); if (!$con) die("Couldn't connect to MySQL"); mysql_select_db("databasename",$con) or die("Couldn't open admin: ".mysql_error()); $q1=mysql_query("SELECT <fieldname> FROM <tablename>"); ?> <select name="<fieldname>"> <? while($row=mysql_fetch_array($q1)) { ?> <option value="<?php echo $row['<fieldname>'];?>"> <?php echo $row['<fieldname>']; ?> </option> <? } ?> </select>
Just remove username,password, databasename, tablename,fieldname as you have.
I hope this will work
•
•
•
•
Originally Posted by clmirand
hi there..
i almost got the same problem.
i have 3 drop down menus in my page.
i was able to populate the first drop down menu.
the 2nd and 3rd drop down menu was not successfully
populated.
php Syntax (Toggle Plain Text)
$con=mysql_connect("localhost","username","password"); if (!$con) die("Couldn't connect to MySQL"); mysql_select_db("databasename",$con) or die("Couldn't open admin: ".mysql_error()); $q1=mysql_query('SELECT table1.col1, table2.col2 FROM `table1`, `table2`'); echo '<select name="<fieldname>">'; while($rowa=mysql_fetch_array($q1)) { echo '<option value="'.$rowa['<fieldname>'].'">'; echo $rowa['<fieldname>'].'</option>'; } echo '</select>'; $q1=mysql_query('SELECT table1.col1, table2.col2 FROM `table1`, `table2`'); echo '<select name="<fieldname>">'; while($rowb=mysql_fetch_array($q1)) { echo '<option value="'.$rowb['<fieldname>'].'">'; echo $rowb['<fieldname>'].'</option>'; } echo '</select>'; $q1=mysql_query('SELECT table1.col1, table2.col2 FROM `table1`, `table2`'); echo '<select name="<fieldname>">'; while($rowc=mysql_fetch_array($q1)) { echo '<option value="'.$rowc['<fieldname>'].'">'; echo $rowc['<fieldname>'].'</option>'; } echo '</select>';
And also, just as an opinion, I find the examples in previous posts to be very basic and very non-php in a way. I believe the echo/print functions are the way to go.
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. - Oopy Doopy Do 2U2!
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - Oopy Doopy Do 2U2!
![]() |
Similar Threads
- 2 questions involving scripts, select tag, and loading pages (PHP)
- php mailto () fuction (PHP)
- 'update' select menu (PHP)
- using Onchange() in PHP (PHP)
- dynamic drop down menu in php (PHP)
- Select tag, making dropdown larger than tag (HTML and CSS)
- AJAX generated <select> and FIREFOX (JavaScript / DHTML / AJAX)
- get html element value using php (PHP)
- select statemt LIKE (MySQL)
Other Threads in the PHP Forum
- Previous Thread: Insert record into db by using ajax post method?
- Next Thread: Hello, someone please HELP me
Views: 2292 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date datepart directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery limit link list login loop mail menu methods mlm mod_rewrite multiple mysql oop parse password paypal pdf php problem query radio random recursion regex remote script search select seo server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web webdesign xml youtube






