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>";
    }
    }
    ?>
0

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

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>";

6 Comments

is your jquery triggered ? try an alert inside ("button").click()
do u have any other solution?
are u getting any error ? see if you are getting the response from the php
i got this code from w3schools.com link is here: "w3schools.com/jquery/tryit.asp?filename=tryjquery_ajax_load"
so what ? just try to print the values on ur php. run ur php file separately and see if you are getting the select box
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.