0

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>";
    }
    }
    ?>
vinus patel
  • 79
  • 1
  • 8

3 Answers3

1

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.

Community
  • 1
  • 1
Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50
0

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>
case1352
  • 1,126
  • 1
  • 13
  • 22
0

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>";
Deepak
  • 6,684
  • 18
  • 69
  • 121