Jump to content

Newbie help needed


bluwe

Recommended Posts

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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!
Link to comment
Share on other sites

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!!
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.