uzair
uzair

Reputation: 766

Select dropdown value to get a populated data from database mysql

I have a populated dropdown list from mysql on one page.

What code should i write in php so that on selection of dropdown list's value, a new page is opened and data is fetched from mysql, provided i have required fields in database.

<form name="form1">
<select>
<?php 
$con=mysql_connect("localhost","FreeUser","123456");
if(!$con)
{
die('Connection failed' . mysql_error());
}
mysql_select_db("human_resource", $con);
$sql1=mysql_query("SELECT eid_emp,name_emp FROM employee_details");
while ($data=mysql_fetch_assoc($sql1))
  {
    ?>
    <option name="drop1" checked value ="<?php echo $data['eid_emp'] ?>" >
    <?php echo $data['name_emp']; ?>
    </option>
  <?php 
  } 
  mysql_close($con);
  ?>
</select>
</form>

Upvotes: 0

Views: 6647

Answers (3)

Joe
Joe

Reputation: 82604

Not a lot of information, but you could do something like this:

<form name="form1" method="POST" action="page.php">
<select onchange="form1.submit()">

Then in the head of your page

<?php 
if(count($_POST))
{
  // do stuff
  header( 'Location: http://somesite.com' ) ;  

Upvotes: 0

John Green
John Green

Reputation: 13435

The answer is there is no PHP code to do what you're asking.

You'll need two things: 1). Some javascript to hook into your form and 2). a way to render the resulting query.

here is some javascript to hook into the form, assuming jquery for brevity (although you should just give the select an ID or a class, which would make the selector less obnoxious):

$('form[name="form1"] select').change(function(){
     location.href='renderer.php?dataKey=' + $(this).val();
});

From there, you'll navigate to renderer.php along with $_GET value for dataKey. render it as your will. If you want to open a new window, use a window.open call instead of setting location.href.

Upvotes: 1

DarkSquirrel42
DarkSquirrel42

Reputation: 10257

has nothing to do with php or mysql ... you could add an "onchange" handler to your < select > element ...

<select onchange="javascript:someFunctionCallHere()">

a call to your forms submit() method should be what you want...

Upvotes: 1

Related Questions