• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1016
  • Last Modified:

Populate Select Menu from JQuery with PHP MySQL query result

Let's say I have a jQuery element (menu select) set up as below:

<div data-role="fieldcontain">
            <select name="selectmenu1" id="selectmenu1">
                <option value="Where MySQL Index Goes">
                    Where MySQL RESULTS GO
                </option>
            </select>
        </div>

Open in new window


And I have some PHP which runs a query to return a list of values like this (this query dumps the results into a table, just to have some place to put it):
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("Mydatabase", $con);

$result = mysql_query("SELECT * FROM wtbl_stuff ORDER BY stuff_id");

echo "<table border='1'>
<tr>
<th>stuff_id</th>
<th>stuff_number</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['stuff_id'] . "</td>";
  echo "<td>" . $row['stuff_number'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?> 

Open in new window


How can I use the PHP MySQL to populate the above Select box in JQuery? I'm sure it's easy, but I've been going around with it for hours with no success. Thanks.
0
JP_TechGroup
Asked:
JP_TechGroup
  • 2
  • 2
1 Solution
 
gr8gonzoConsultantCommented:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("Mydatabase", $con);

$result = mysql_query("SELECT * FROM wtbl_stuff ORDER BY stuff_id");
?>

<div data-role="fieldcontain">
            <select name="selectmenu1" id="selectmenu1">
<?php
while($row = mysql_fetch_array($result))
  {
  echo "<option value='" . $row['stuff_id'] . "'>";
  echo $row['stuff_number'];
  echo "</option>";
  }
?>
            </select>
        </div>
0
 
JP_TechGroupAuthor Commented:
That's actually what I had, but the menu box does not populate,  I know the connection and query works, what am I missing?
0
 
gr8gonzoConsultantCommented:
What is the resulting HTML source?
0
 
Mark BradyCommented:
I would try it like this.

<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("Mydatabase", $con);

// create the select element
$select = '<select name="selectmenu1" id="selectmenu1">';

// run the query
$result = mysql_query("SELECT * FROM wtbl_stuff ORDER BY stuff_id");

// loop the result set for items to populate select element
while($row = mysql_fetch_array($result)) {
    $select .= "<option value='".$row['stuff_id']."'>";
    $select .= $row['stuff_number'];
    $select .= "</option>";
  }

// close out the select
$select .= "</select>";
?>

<div data-role="fieldcontain"><?php echo $select; ?></div>

Open in new window


If it doesn't work, check there is not another variable $result anywhere after your mysql call that would modify the values inside $result. Also, you could var_dump($select); if you use my suggestion above then see if everything looks correct.

Your div has no id or name. Are you positive there is nothing wrong with it and it displays properly if you put a simple sample select element inside it instead of the database results?

Check those things and let me know what you find.
0
 
JP_TechGroupAuthor Commented:
Found it. Quite silly really. Had the page saved as HTML not PHP.
Half a day of my life wasted!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now