943,810 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 9914
  • PHP RSS
Dec 22nd, 2008
0

Using <select> tag with PHP

Expand 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:
PHP Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ckurtz is offline Offline
21 posts
since Dec 2007
Dec 23rd, 2008
0

Re: Using <select> tag with PHP

Hi,
This is very simple. I can help You. Just follow the code given below

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ashafaaiz is offline Offline
20 posts
since Dec 2008
Dec 23rd, 2008
0

Re: Using <select> tag with PHP

Click to Expand / Collapse  Quote originally posted by ashafaaiz ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ckurtz is offline Offline
21 posts
since Dec 2007
Dec 23rd, 2008
0

Re: Using <select> tag with PHP

Click to Expand / Collapse  Quote originally posted by ckurtz ...
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)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ckurtz is offline Offline
21 posts
since Dec 2007
Mar 7th, 2009
0

Re: Using <select> tag with PHP

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



================================================
Click to Expand / Collapse  Quote originally posted by ashafaaiz ...
Hi,
This is very simple. I can help You. Just follow the code given below

php Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
clmirand is offline Offline
5 posts
since Feb 2008
Mar 8th, 2009
0

Re: Using <select> tag with PHP

Quote 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:
php Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Insert record into db by using ajax post method?
Next Thread in PHP Forum Timeline: Hello, someone please HELP me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC