0

I want to fetch data from dropdown list. Like if I choose employee id 40 from the dropdown list it will fetch the data from database of that employee and will shown in the textboxes.

this is my dropdown code. please help me how can i get the selected value.

<?php
    $con=mysqli_connect("localhost","root","","hct_db");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
?>

 <label>Select Employee ID</label>
     <select class="form-control" name="employee_id">
         <?php 
         $result = mysqli_query($con,"SELECT employee_id FROM employee order by employee_id");

         while($row = mysqli_fetch_array($result)) 
             echo "<option value='" . $row['employee_id'] . "'>" . $row['employee_id'] . "</option>";
         ?>
     </select>
Luke
  • 22,826
  • 31
  • 110
  • 193
AakkiRock
  • 288
  • 1
  • 4
  • 14
  • There isn't any textbox in your code – Luke Sep 29 '14 at 10:18
  • 2
    Do you have knowledge of jquery and ajax? – Indrasinh Bihola Sep 29 '14 at 10:19
  • you can use `onchange` of `jquery` to check when ever you select any value it will `trigger onchange` method and then via Ajax you can fill values in your `textareas` – Naveed Yousaf Sep 29 '14 at 10:19
  • Are you using any javascript libraries at all (e.g., jQuery, prototype, MooTools, etc)? – daiscog Sep 29 '14 at 10:22
  • I know the coding of that but my problem is to get the dropdown value which is selected. like in the sql coding what i should write ? select [dropdwon value] from employee ?? – AakkiRock Sep 29 '14 at 10:22
  • In a jQuery event handler, you would use `$(this).val()`. Show your JavaScript code for more specific help. – daiscog Sep 29 '14 at 10:23
  • So your problem is on the server-side? The SQL is quite straight-forward; something like: `"SELECT * FROM employee WHERE employee_id = " + intval($_GET['employee_id'])`. (The `inval` call is a quick-and-dirty validation to prevent SQL injection; use proper validation with error handling, if necessary.) – daiscog Sep 29 '14 at 10:29

6 Answers6

1
<?php
if(isset($_REQUEST['submit']))
{
  $value=$_POST['employee_id'];
  $query = mysql_query($con,"SELECT employee_name FROM employee where employee_id=$value");
  $result=mysql_fetch_array($query);
  $emp_name=$result['employee_name'];
}
?>

<form action="" method="post" name="form">
 <label>Select Employee ID</label>
   <select class="form-control" name="employee_id">
    <?php $result = mysqli_query($con,"SELECT employee_id FROM employee order by employee_id");
 while($row = mysqli_fetch_array($result)) 
   echo "<option value='" . $row['employee_id'] . "'>" .$row['employee_id'] . "</option>";
 ?>
  </select>
<input type="submit" name="submit" value="submit">
</form>
 <input type="text" value="<?=$emp_name?>" name="emp_name"/>

check this code as your need
Alok Gupta
  • 81
  • 7
  • GET should really be used for this, not POST. Also, the query at the top isn't actually selecting the requested employee, but is selecting the employee with the smallest employee_id (you've missed out the WHERE clause). – daiscog Sep 29 '14 at 10:36
  • Oh, and you've used the deprecated mysql_* functions, whereas the OP is correctly using mysqli. – daiscog Sep 29 '14 at 10:42
1

First of all give some id to you select option like this:

<select class="form-control" name="employee_id" id='employee'>

Add you textbox like this:

<input type='text' name='emp_name' id='emp_name' />

Than use jquery and ajax something like this:

$('#employee').change(function(){
    var selected_id = $(this).val();
    var data = {id:selected_id};
    $.post('getemp_name.php',data,function(data){
         $('#emp_name').val(data);
    });
});

getemp_name.php

if(isset($_POST['id'])){
    //fire query using this id and get the name of employee and echo it
    echo $emp_name;
}
Indrasinh Bihola
  • 2,094
  • 3
  • 23
  • 25
0

To get your selected value, you have to reach the $_GET or $_POST supertables after the user submits the form.

So, after the submit, get it as, if you POST:

<?php 
$employee_id = $_POST['employee_id']; ?>

If you GET:

<?php 
$employee_id = $_GET['employee_id']; ?>
Balrog2000
  • 16
  • 1
0

apply below function on onChange

function myFunction(mySelect) {
        var x = document.getElementById("mySelect").value;
        document.getElementById("demo").innerHTML = "You selected: " + x;
    }

example

Community
  • 1
  • 1
Prashant Tapase
  • 2,132
  • 2
  • 25
  • 34
0

That depends on what you want. If you want to use the selected ID in your query AFTER DOING A POST, you can use the following query:

    "SELECT   *
     FROM     `employee`
     WHERE    `employee`.`employee_id` = " . (int) $_POST['employee_id'] . "
     LIMIT    1"

Assuming you are using the post method in your form.

0

You can easily learn how to fetch data from dropdown using this example by clicking Here This repository contains all the codes

Malith Ileperuma
  • 926
  • 11
  • 27