vinus patel
vinus patel

Reputation: 79

populate dropdown list using jquery load event

I want to populate dropdown value from mysql table.I used load event in html page,but i don't know this code doesn't work.

HTML PAGE

<!DOCTYPE html>
    <html>
    <head>
    <script src="jquery.js"></script>
    <script>
    $(document).ready(function(){
      $("button").click(function(){
        $("#select1").load("se2.php");
      });
    });
    </script>
    </head>
    <body>

    <select id="select1"></select>
    <button>Get External Content</button>

    </body>
    </html>

se2.php

 <?php
    $dbhandle = mysql_connect("localhost","root","") 
     or die("Unable to connect to MySQL");
    $selected = mysql_select_db("student",$dbhandle) 
      or die("Could not select examples");
    $result1 = mysql_query("SELECT column1 FROM information");
     while($row=mysql_fetch_array($result1))
    {
   if($row['column1']!=NULL)
    {
    echo "<option value='$row[column1]'>$row[column1]</option>";
    }
    }
    ?>

Upvotes: 0

Views: 2819

Answers (3)

Mehdi Karamosly
Mehdi Karamosly

Reputation: 5438

If you want to do the logic in the front end using jquery, you can follow this example :

jQuery: Best practice to populate drop down?

This way you can return your php array using json_encode() and in you jquery function you access it as an object, like in the above example.

Upvotes: 1

Deepak
Deepak

Reputation: 6812

In HTML instead of including <select> try this,

<div id="select1"></div>

And in php try this,

echo "<select>";
while($row=mysql_fetch_array($result1))
{
   if($row['column1']!=NULL)
   {
     echo "<option value='$row[column1]'>$row[column1]</option>";
   }
}
echo "</select>";

Upvotes: 0

case1352
case1352

Reputation: 1136

change this

 <select id="select1"></select>

to this

<div id="select1"></div>

and add the select tags to the php

 $result1 = mysql_query("SELECT column1 FROM information");
echo "<select>";
     while($row=mysql_fetch_array($result1))
    {
   if($row['column1']!=NULL)
    {
    echo "<option value='$row[column1]'>$row[column1]</option>";
    }
    }
echo "</select>";

and give the button an id

<button id="button">Get External Content</button>

Upvotes: 0

Related Questions