bluwe Posted November 9, 2006 Share Posted November 9, 2006 Hi,I'm trying to create a dynamic drop down box from values in a table. I than want to echo out the value selected. Here's the code:<html><head><title>Unassigned Books</title></head><body><? require 'db.inc';$query = "Select book_id from books where book_status = 'Available' ";$result = mysql_query($query) or die ("Could not execute query.");/* Create drop down box*/echo "<form action='process.php' method='POST'> <select name='selected_book'>\n";while ($row = mysql_fetch_array($result)){ extract ($row); echo "<option value='$book'>$card_id\n";}echo "</select>\n";echo "<input type='submit' value='Pick Card'></form>\n";?></body></html>My process.php is as follows:<?foreach ($_POST as $field => $value){echo "$field = $value<br>";}?></body></html>I'm basically getting a blank value echoed out. If I add other inputs into the main script I can echo them out using process.php, but not any value I select from the drop down. This is doing my head in!TIA Quote Link to comment Share on other sites More sharing options...
haaglin Posted November 9, 2006 Share Posted November 9, 2006 [code]echo "<option value='$book'>$card_id\n";[/code]should be:[code]echo "<option value='$book'>$card_id</option>\n";[/code] Quote Link to comment Share on other sites More sharing options...
jcbarr Posted November 9, 2006 Share Posted November 9, 2006 hmmm...why are you using extract? You have no array to import variables from. Also you are trying to echo a variable that hasn't been defined.[code]while ($row = mysql_fetch_array($result)){ extract ($row); echo "<option value='$book'>$card_id\n";}[/code]Should be something like this[code]while ($row = mysql_fetch_array($result)){ $card_id=$row['card_id']; $book=$row['book']; echo "<option value='$book'>$card_id\n";}[/code]Also you dont' need the </option> tag. The next <option> tag will automatically end the first one. Quote Link to comment Share on other sites More sharing options...
bluwe Posted November 9, 2006 Author Share Posted November 9, 2006 Sorry that should have read:<html><head><title>Unassigned Books</title></head><body><?require 'db.inc';$query = "Select book_id from books where book_status = 'Available' ";$result = mysql_query($query) or die ("Could not execute query.");/* Create drop down box*/echo "<form action='process.php' method='POST'> <select name='selected_book'>\n";while ($row = mysql_fetch_array($result)){ extract ($row); echo "<option value='$book'>$book_id\n";}echo "</select>\n";echo "<input type='submit' value='Pick Book'></form>\n";?></body></html> Quote Link to comment Share on other sites More sharing options...
haaglin Posted November 9, 2006 Share Posted November 9, 2006 if you want $book and $book id, you need book in your query:$query = "SELECT book,book_id FROM books WHERE book_status = 'Available' ";and don't forget to add </option> at the end of each option. Quote Link to comment Share on other sites More sharing options...
bluwe Posted November 9, 2006 Author Share Posted November 9, 2006 What I want is to populate the drop down with a list of books in the table that have a status of available. Then I want to echo out whatever the user selects from the drop down. the process.php is just to test what I'm getting really. Sorry for being so vague I'm new to all this! Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 9, 2006 Share Posted November 9, 2006 [quote author=jcbarr link=topic=114465.msg465682#msg465682 date=1163113973]Also you dont' need the </option> tag. The next <option> tag will automatically end the first one.[/quote]</option> is XHTML friendly... though he isn't using XHTML</option> is a good practice though IMO Quote Link to comment Share on other sites More sharing options...
sws Posted November 9, 2006 Share Posted November 9, 2006 Wow, I'm looking to do the exact same thing. Except instead of echo ing the selected value, I need the selected values to be inserted into a db table and emailed to me as well. Quote Link to comment Share on other sites More sharing options...
bluwe Posted November 9, 2006 Author Share Posted November 9, 2006 Well, eventually I want to do something constructive with the value selected but I can't even echo out what the user selected yet so I'm stuck Quote Link to comment Share on other sites More sharing options...
haaglin Posted November 9, 2006 Share Posted November 9, 2006 in process.php: echo $_POST['selected_book'];thats the selected value. Quote Link to comment Share on other sites More sharing options...
bluwe Posted November 9, 2006 Author Share Posted November 9, 2006 Sorry haaglin, that didn't work. however I tried this and it worked.I changed this:{ extract ($row); echo "<option value='$book'>$book_id\n";}To this:{ extract ($row); echo "<option value='$book_id'>$book_id\n";}I wish I knew what I'd done! Thanks everyone for helping!! Quote Link to comment Share on other sites More sharing options...
haaglin Posted November 10, 2006 Share Posted November 10, 2006 The difference is what i explained earlier. You only selected book_id in your query. [code]$query = "SELECT book_id FROM books WHERE book_status = 'Available' ";[/code]need to be:[code]$query = "SELECT book,book_id FROM books WHERE book_status = 'Available' ";[/code]if you did't change that, then the variable $book is empty[code]echo "<option value='$book'>$book_id\n";[/code]As the option value was empty, so was the $_POST[selected_book]. When you added $book_id instead of $book. The value was not empty anymore. Quote Link to comment Share on other sites More sharing options...
bluwe Posted November 10, 2006 Author Share Posted November 10, 2006 Cheers Haaglin I really appreciate your help!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.