【问题标题】:selecting related data from database with select option, php mysql使用选择选项从数据库中选择相关数据,php mysql
【发布时间】:2018-11-28 15:02:08
【问题描述】:

数据库表“Product”有一个“Product_Code”和“Product_Name”

我们有一个填写产品数据的表格

从数据库表列“Product_Code”中获取选择选项

<select name="Select_Product_Code" id="Select_Product_Code"> 
    <option value="0">Product</option>
        <?php
             $con = mysqli_connect('localhost','user1db','userdb','1db');
    if (!$con) {    die('Could not connect: ' . mysqli_error($con));    }
    mysqli_select_db($con,"ajax_demo");
    $sql="SELECT * FROM Product";
    $result = mysqli_query($con,$sql);
    while($row = mysqli_fetch_array($result)) 
            {
            ?>
            <option value = "<?php echo($row['Product_Code'])?>" >
                <?php echo($row['Product_Code']);
                 ?>
            </option>
            <?php 
            } 
            ?>
    </select>

如果不提交表单,有没有办法在选择“Product_Code”时在 Label 或 TextInput 中显示“Product_Name”?

编辑,添加 ajax。

readproduct.php

<!DOCTYPE html>
<html>
<head>
<script>
function showProduct(str) {
  if (str=="") {
        document.getElementById("txtHint").innerHTML="";
    return;
  } 
  if (window.XMLHttpRequest) {
            xmlhttp=new XMLHttpRequest();
  } 
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
      document.getElementById("txtHint").innerHTML=this.responseText;
    }
  }
  xmlhttp.open("GET","getproduct.php?q="+str,true);
  xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="products" onchange="showProduct(this.value)">
<option value="">Select </option>
<option value="1">0001</option>
<option value="2">0002</option>
<option value="3">0003</option>
<option value="4">0004</option>
</select>
</form>
<br>
<div id="txtHint"><b>list</b></div>
</body>
</html>

getproduct.php如下

    <?php
    $q = intval($_GET['q']);
    $con = mysqli_connect('localhost','user1db','userdb','1db');
    if (!$con) 
    {
        die('Could not connect: ' . mysqli_error($con));
    }
    echo "Connected";
    mysqli_select_db($con,"ajax_demo");
    $sql="SELECT * FROM Stock WHERE Product_Code = '".$q."'";
    $result = mysqli_query($con,$sql);
    while($row = mysqli_fetch_array($result)) 
    {
        echo $row['Product_Name'];
    }
    mysqli_close($con);
?>

如果我们删除 where 子句,则显示所有产品名称, 使用 where 子句,getproduct.php 不显示 Product_Names。 我们错过了什么或做错了什么?

【问题讨论】:

  • 你可以很容易地用jquery展示这个
  • 查看 AJAX。
  • 要么在页面上构建元素,在做出选择之前隐藏,然后显示您想要的标签/输入......或使用 AJAX 并选择相关的数据库数据。
  • 我们用过ajax php, xmlhttp.open("GET","getproduct.php?q="+str,true);连接数据库但没有传“q”,或者可能是getproduct.php编码错误。
  • @MD.JubairMizan 你能给我链接小提琴或例子吗?

标签: php mysql html


【解决方案1】:

您可以在 while 循环中填充一个数组,稍后将在 select 元素的更改侦听器中使用该数组,如下所示:

<select name="Select_Product_Code" id="Select_Product_Code"> 
    <option value="0">Product</option>
        <?php
             $con = mysqli_connect('localhost','user1db','userdb','1db');
    if (!$con) {    die('Could not connect: ' . mysqli_error($con));    }
    mysqli_select_db($con,"ajax_demo");
    $sql="SELECT * FROM Product";
    $result = mysqli_query($con,$sql);
    while($row = mysqli_fetch_array($result)) 
            {

// here it adds a name with a key that matches the option-value
            $names[$row['Product_Code']] = $row['Product_Name'];

            ?>
            <option value = "<?php echo($row['Product_Code'])?>" >
                <?php echo($row['Product_Code']);
                 ?>
            </option>
            <?php 
            } 
            ?>
    </select>

<input type="text" id="out-text" value="The Name will appear here" />

<!-- then it populates the array with json_encode() to be used in the event-listener (both below) -->
<script type="text/javascript">
var names = <?php echo json_encode($names); ?>;

document.getElementById("Select_Product_Code").addEventListener(
     'change',
     function() { 
        document.getElementById("out-text").value = names[this.selectedIndex]; 
     },
     false
  );
</script>

这不是唯一的方法,而是它的 vanilla javascript/php,以及带有基本逻辑的here is a JSFiddle(减去 PHP 部分)

【讨论】:

  • 谢谢,它适用于 js,但我无法使其适用于 php mysql,将再次检查我的代码。
猜你喜欢
  • 2017-02-04
  • 2019-12-04
  • 2012-01-20
  • 2016-12-17
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多