Using <select> tag with PHP

Reply

Join Date: Dec 2007
Posts: 20
Reputation: ckurtz is an unknown quantity at this point 
Solved Threads: 0
ckurtz ckurtz is offline Offline
Newbie Poster

Using <select> tag with PHP

 
0
  #1
Dec 22nd, 2008
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:
  1. <select name="project">
  2. <option value="">-Select one-</option> -->
  3. <?php
  4. while($proj = mysql_fetch_array($qryS)){
  5. $id = $proj['id'];
  6. $project = $proj['project'];
  7.  
  8. $i = $i + 1;
  9. echo "<option value=\"$i\"";
  10. echo ($i == $id) ? "SELECTED":"";
  11. echo ">" . $project . "</option>";
  12.  
  13. }
  14.  
  15. ?>
  16. </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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 19
Reputation: ashafaaiz is an unknown quantity at this point 
Solved Threads: 1
ashafaaiz ashafaaiz is offline Offline
Newbie Poster

Re: Using <select> tag with PHP

 
0
  #2
Dec 23rd, 2008
Hi,
This is very simple. I can help You. Just follow the code given below

  1. $con=mysql_connect("localhost","username","password");
  2. if (!$con)
  3. die("Couldn't connect to MySQL");
  4. mysql_select_db("databasename",$con)
  5. or die("Couldn't open admin: ".mysql_error());
  6. $q1=mysql_query("SELECT <fieldname> FROM <tablename>");
  7. ?>
  8. <select name="<fieldname>">
  9. <?
  10. while($row=mysql_fetch_array($q1))
  11. {
  12. ?>
  13. <option value="<?php echo $row['<fieldname>'];?>">
  14. <?php
  15. echo $row['<fieldname>'];
  16.  
  17. ?>
  18. </option>
  19. <?
  20. }
  21. ?>
  22. </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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 20
Reputation: ckurtz is an unknown quantity at this point 
Solved Threads: 0
ckurtz ckurtz is offline Offline
Newbie Poster

Re: Using <select> tag with PHP

 
0
  #3
Dec 23rd, 2008
Originally Posted by ashafaaiz View Post
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
i better try this now. thanks
pLan yOur wOrK and wOrK yoUr pLan
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 20
Reputation: ckurtz is an unknown quantity at this point 
Solved Threads: 0
ckurtz ckurtz is offline Offline
Newbie Poster

Re: Using <select> tag with PHP

 
0
  #4
Dec 23rd, 2008
Originally Posted by ckurtz View Post
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:
  1. <select name="project">
  2. <option value="">-Select one-</option> -->
  3. <?php
  4. while($proj = mysql_fetch_array($qryS)){
  5. $id = $proj['id'];
  6. $project = $proj['project'];
  7.  
  8. $i = $i + 1;
  9. echo "<option value=\"$i\"";
  10. echo ($i == $id) ? "SELECTED":"";
  11. echo ">" . $project . "</option>";
  12.  
  13. }
  14.  
  15. ?>
  16. </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
thank you for this code. I tried it and the result is still the same. I just want that the value of the selected option will show in the <select> tag when i submit the form. it will stay in the same as i chose from the selected option.
pLan yOur wOrK and wOrK yoUr pLan
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: clmirand is an unknown quantity at this point 
Solved Threads: 0
clmirand clmirand is offline Offline
Newbie Poster

Re: Using <select> tag with PHP

 
0
  #5
Mar 7th, 2009
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



================================================
Originally Posted by ashafaaiz View Post
Hi,
This is very simple. I can help You. Just follow the code given below

  1. $con=mysql_connect("localhost","username","password");
  2. if (!$con)
  3. die("Couldn't connect to MySQL");
  4. mysql_select_db("databasename",$con)
  5. or die("Couldn't open admin: ".mysql_error());
  6. $q1=mysql_query("SELECT <fieldname> FROM <tablename>");
  7. ?>
  8. <select name="<fieldname>">
  9. <?
  10. while($row=mysql_fetch_array($q1))
  11. {
  12. ?>
  13. <option value="<?php echo $row['<fieldname>'];?>">
  14. <?php
  15. echo $row['<fieldname>'];
  16.  
  17. ?>
  18. </option>
  19. <?
  20. }
  21. ?>
  22. </select>

Just remove username,password, databasename, tablename,fieldname as you have.
I hope this will work
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,472
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: Using <select> tag with PHP

 
0
  #6
Mar 8th, 2009
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.
Well you have only provided one query there so that would be why only one menu is working. Does your script look something like the following:
  1. $con=mysql_connect("localhost","username","password");
  2. if (!$con) die("Couldn't connect to MySQL");
  3. mysql_select_db("databasename",$con) or die("Couldn't open admin: ".mysql_error());
  4. $q1=mysql_query('SELECT table1.col1, table2.col2 FROM `table1`, `table2`');
  5. echo '<select name="<fieldname>">';
  6. while($rowa=mysql_fetch_array($q1))
  7. {
  8. echo '<option value="'.$rowa['<fieldname>'].'">';
  9. echo $rowa['<fieldname>'].'</option>';
  10. }
  11. echo '</select>';
  12.  
  13.  
  14. $q1=mysql_query('SELECT table1.col1, table2.col2 FROM `table1`, `table2`');
  15. echo '<select name="<fieldname>">';
  16. while($rowb=mysql_fetch_array($q1))
  17. {
  18. echo '<option value="'.$rowb['<fieldname>'].'">';
  19. echo $rowb['<fieldname>'].'</option>';
  20. }
  21. echo '</select>';
  22.  
  23.  
  24. $q1=mysql_query('SELECT table1.col1, table2.col2 FROM `table1`, `table2`');
  25. echo '<select name="<fieldname>">';
  26. while($rowc=mysql_fetch_array($q1))
  27. {
  28. echo '<option value="'.$rowc['<fieldname>'].'">';
  29. echo $rowc['<fieldname>'].'</option>';
  30. }
  31. echo '</select>';
Note: In the above example replace <fieldname> with the real column name.
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.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC