0

我想在用户从下拉列表中选择一个选项后显示我的数据库中的详细信息。就像是 :

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>
4

3 回答 3

1

它的简单使用 Ajax,我已经完成了下面的检查

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

//并且不要忘记包含Jquery文件...

<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>
于 2013-03-08T11:57:40.193 回答
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:停止使用mysql_*功能。为什么?在这里找到

于 2013-03-08T12:04:39.043 回答
0

这是正确的代码:

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

然后它将显示来自数据库的详细信息

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
于 2013-03-08T12:06:14.747 回答