Useing PHP and mySQL to populate listbox?

My experience with PHP until this point was with “PHP only” scripts (no HTML included in code). I’ve now built my page in Dreamweaver so my form looks pretty, but I guess pretty doesn’t mean much if I don’t know how to make it work. So, I need some help with a few things that have been driving me nuts.

I have 2 questions regarding PHP’s interaction with forms.

On my page I have 3 radio buttons (first,after,last)
Next to the “after” radio button I have a drop-down listbox (after_list)

Maybe my first question doesn’t involve PHP but how do I go about making the “after” radio button select the “after_list” listbox?

My second question regards embeding PHP into HTML. How would I write the PHP code into my Dreamweaver HTML to querry the mySQL server and populate the listbox with a columns’ information?

Any help would be greatly appreciated.

Thanks,
JOSH

Originally posted by josh013
[B]Maybe my first question doesn’t involve PHP but how do I go about making the “after” radio button select the “after_list” listbox?

My second question regards embeding PHP into HTML. How would I write the PHP code into my Dreamweaver HTML to querry the mySQL server and populate the listbox with a columns’ information?
[/B]

1st question:
use javascript… PHP can’t do that for you unless you want to display the page again (which means reloading the page)

2nd question:
what do you have so far? you just simply type out the PHP code in the coding window…

the basic idea is this (in pseudocode)


connect to mysql server (using mysql_connect())
select the db you want to perform your query (using mysql_select_db())
retrieve required rows from db (using mysql_query())
while($row =  fetched row)
{
  display data
}

You could populate a javascript array with php information (well mysql information)…then use the onfocus event on the radio button. When the ‘last’ button it will either display the correct drop down list or fill a current list there. So really it can be a combination of both :smiley:

Originally posted by Maelstrom
So really it can be a combination of both :smiley:

can’t help it… i’ve to defend myself :smiley:

what i meant was that you can’t use PHP to dynamically select the after_listbox when you click the after radio button, without reloading the page…

and yes of course you can use a combination of JS and PHP… :smiley:

I’m new to PHP, so I appologize for funny questions.
So far this is the PHP script that I’ve been trying to use, it currently sits within the form tags on the page but the form itself is submitted through an external PHP script.

<?

  // Attemp connection to MySQL server
  $link = @mysql_connect("localhost");

        if ($link == false) {
           echo "&result=Fail&errormsg=";
           echo urlencode("Failed to connect to MySQL server.");
           echo "&";
           exit;
        }

  // Attemp to select out DB
  if (mysql_select_db("quiz") == false) {
  echo "&result=Fail&errormsg=";
  echo urlencode("Error selecting database.\
");
  echo urlencode("Error: " . mysql_error($link));
  echo "&";
  exit;
  }

//query the DB
$query = mysql_query("SELECT Title, OrderID from lessons ORDER BY OrderID");

//build the drop down list
print '<select name="title">';
while ($row = mysql_fetch_array($query)) {
printf('<option value="%s">%s', $row["Title"], $row["OrderID"]);
}
print '</select>';

?>

Don’t know if any of it’s right, I havent gotten this to work yet.(Had no clue on how to populate a listbox and found a post on this board doing something similar, so I’ve been trying to modify this code to fit my needs)

http://www.sitepointforums.com/showthread.php?threadid=12402&highlight=drop+down+list+php+mysql

Do I need to use a certain type of tag? Or does the PHP script need to exist outside the form tags?
Also I am attempting to use “OrderID” as the selected “Title” value.
Thanks again for any ideas and help.
JOSH

Originally posted by josh013
Do I need to use a certain type of tag? Or does the PHP script need to exist outside the form tags?

what do you mean a certain type of tag :confused:

you can mix and match php and html by writing your php code withing the <?php and ?> tags… outside these tags just use normal html…

Ok, I understand that PHP can be mixed in with HTML, I thought maybe there was something wrong with how I was writing it.
If how and where I’m writting it is ok, what did I screw up in my codeing, can anyone see the errors in this, and if so could you point them out::confused:
Thanks for the help!
JOSH

  <select name="Title" size="1">
  <option value="">' Select A Lesson '</option>

  <?php


  // Attemp connection to MySQL server

  $link = @mysql_connect("localhost");

        if ($link == false) {
           echo "&result=Fail&errormsg=";
           echo ("<p>Failed to connect to MySQL server.</p>");
           echo "&";
           exit;
        }

  // Attemp to select DB
  if (mysql_select_db("quiz") == false) {
  echo "&result=Fail&errormsg=";
  echo ("<p>Error selecting database."\
 . "</p>");
  echo ("<p>Error: " . mysql_error($link). "</p>");
  exit;

  // php to select dropdown list options from table

  $query = @mysql_query("SELECT Title, OrderID FROM quiz ORDER BY OrderID");
  if (!$query) {
  echo("<p>Error performing query: " . mysql_error() . "</p>");
  exit();
  }

  // build the dropdown list

  while($row = mysql_fetch_array($query)) {

  $title=$row["Title"];
  $orderid=$row["OrderID"];

  echo '<option value="' . $orderid . '">' . $title . '</option>';

  }
  ?>
  </select>

echo ‘<option value="’ . $orderid . ‘">’ . $title . ‘</option>’;

to

echo “<option value=‘$orderid’>$title</option>”;

Originally posted by redgoals
[B]echo ‘<option value="’ . $orderid . ‘">’ . $title . ‘</option>’;

to

echo “<option value=‘$orderid’>$title</option>”; [/B]
I have to disagree - using single quotes in HTML has always seemed pretty iffy to me (I’m not sure that the w3c standard even allows for that kind of code) and avoiding double quote string processing is better for performance.

Josh - what error messages are you getting?

Looking at the code your problem could be that you are not supplying the username and password for your mySQL connection with mysql_connect().

Skunk,
I’ve never had to supply a username and password before,AAAHHHHH! <-“Head explodes”

I’m not getting any error messages, the options that are returned in the list box are as follows:

‘Select A Lesson’
’ . $title . ’

Like it’s ignoring my PHP tags hence my earlier questions.

If my installation of mySQL is set to connect without the use of a username and/or password (in other words the username and password are not set - Win32 binary), should that make a difference?

Does the code its self look alright?
Thanks,
JOSH

try this… though your original code looks alright but i may be missing something (can’t quite tell with all those quotes heh :smiley:


echo "<option value=" . $orderid. ">" . $title. "</option>"; 

and yes, do please use double quotes to surround your tag attribute values… it’s more standards-compliant

Doh!
Feel like a moron.
Scripts work better when saved as .php :wall: (not .htm)
Thank you all so much for the help, I’m gonna go crawl under a rock now.
JOSH