0

I want to display details from my database after a user selects an option from the dropdown. Something like :

SELECT `details` FROM $tbl_name WHERE id= value of selected option


<?php



    $db_name="pet"; // Database name
    $tbl_name="lover"; // Table name

    $pet_id=$_POST['pet_id']                        
    $sql="SELECT details FROM $tbl_name WHERE id = $pet_id";
    $result=mysql_query($sql);
    while($rows=mysql_fetch_array($result)){
    $id=$rows['id'];
    $details=$rows['details'];

    // close while loop 
    }

 ?>
<select name="pet_id">
    <option value="">Select PET:</options>
    <option value="1">DOG</options>
    <option value="2">BIRD</options>
    <option value="3">FISH</options>

    <a> then it will display details from database</a>
asprin
  • 9,579
  • 12
  • 66
  • 119
  • Try it with AJAX. Or echo them in hidden `

    ` tags and show/hide with JavaScript

    – AmazingDreams Mar 08 '13 at 11:54
  • Use javascript to fire a `onchange` function. In that function, add the selected value as the query string. Then grab that value through `$_GET` and use that in your `WHERE` condition. PS : Do not forget to sanitize the data. – asprin Mar 08 '13 at 11:59
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – h2ooooooo Mar 08 '13 at 12:00

3 Answers3

1

Its simple use Ajax, i have done below check

db_file.php

<?php
$db_name="pet"; // Database name
$tbl_name="lover"; // Table name

$pet_id=$_POST['pet_id']                        
$sql="SELECT details FROM $tbl_name WHERE id = $pet_id";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
$id=$rows['id'];
$details=$rows['details'];

echo $details;//this output is transeffered to ur actual page
}

?>

current_file.php

//And dont forget to include Jquery file...

<script>
$("#pet_id").change(function()
    {

        var value = $("#pet_id option:selected").text();

        $.ajax({ 
            url: "current_file.php",
            data: {value: value},
            type: "POST",
            success: function(output) {
                    alert(output);//Here u wil get your output, You can append this where you want to display
                   }
            }
        })

});
</script>
<select name="pet_id" id ="pet_id" >
<option value="">Select PET:</options>
<option value="1">DOG</options>
<option value="2">BIRD</options>
<option value="3">FISH</options>
Php Geek
  • 1,107
  • 1
  • 15
  • 36
  • thanks for the reply pavan k but the code got error Results PHP Syntax Check: Parse error: syntax error, unexpected T_VARIABLE in your code on line 16 PHP Syntax Check: Errors parsing your code – asiong_salonga Mar 08 '13 at 13:35
0

HTML

<select name="pet_id" onchange="redirect(this.value)">
    <option value="">Select PET:</options>
    <option value="1">DOG</options>
    <option value="2">BIRD</options>
    <option value="3">FISH</options>
</select>

JAVASCRIPT

function redirect(selectedOption)
{
  if(selectedOption)
  {
     window.location = 'yourpagename.php?opt='+selectedOption;
  }
  else
  {
     window.location = 'yourpagename.php';
  }
}

PHP

if(isset($_GET['opt']))
{
  $opt = $_GET['opt'];  // do not forget to sanitize this
  $sql="SELECT details FROM $tbl_name WHERE id = $opt";
  $result=mysql_query($sql);
  while($rows=mysql_fetch_array($result)){
  $id=$rows['id'];
  $details=$rows['details'];
  // do something with $details; in your case echo it out      
  }
}

PS : Stop using mysql_* functions. Why? Find out HERE

Community
  • 1
  • 1
asprin
  • 9,579
  • 12
  • 66
  • 119
0

This is the correct code:

<form action="$_SERVER['PHP_SELF']" method="POST">
<select name="pet_id" id="pet_id" onChange="this.form.submit()">
    <option value="">Select PET:</options>
<option value="1">DOG</options>
<option value="2">BIRD</options>
<option value="3">FISH</options>
</select>
</form>

then it will display details from database

PHP

$pet_id=$_POST['pet_id']; 

if (!empty($pet_id)){

    $db_name="pet"; // Database name
    $tbl_name="lover"; // Table name


    $sql="SELECT details FROM $tbl_name WHERE id = $pet_id";
    $result=mysql_query($sql);
    while($rows=mysql_fetch_array($result)){
        $id=$rows['id'];
        $details=$rows['details'];

       // close while loop 
     }
}//close if statement
AmazingDreams
  • 3,136
  • 2
  • 22
  • 32