链接数据选择选项与ajax& PHP

时间:2016-10-14 20:50:16

标签: php mysql ajax

我正在尝试从材料编号填充材料描述。 Bot hte值存储在相同的SQL中。所以我选择Material Maiterial Description shpul auto populate时我想要的。

表中提交的材料是材料和材料。 MaterialDescripotion

以下是主文件中提取数据的代码

<?php
include_once "dbConnect.php";

$sql = "SELECT * FROM DRLINK";

$result2 = mysqli_query($conn, $sql);
if (!$result2) {
    printf("Error: %s\n", mysqli_error($conn));
    exit();
}
$options = "";
while($row2 = mysqli_fetch_array($result2))
{
$options = $options."<option>$row2[1]</option>";

}


?>
<html>
<!DOCTYPE html>

<head>
<title>Dropdown Ajax</title>
</head>
<body>

<form action ="DSSTRsubmit.php" method="POST">
<table border="1">
<tr>
<td>Select Retailer</td>
</tr>
<tr>
<td>
<?php
echo "<select>";
 echo $options;
   echo "</select>";
 ?>

</td> 
</tr>
</table>

<br/>
<br/>
<br/>




<table border="1">

<tr>
<td>Material</td>
<td>Material Description</td>
<td>Quantity</td>
<td>Unit of Measure</td>
</tr>
<tr>

<td>
<div class="Material">

<select name="Material" onchange="getId(this.value);">
<option value="">Select Country</option>
<?php
$query ="SELECT * FROM MATERIALLIST";
$results = mysqli_query($conn, $query);
foreach($results as $MATERIALLIST) {
    ?>


<option value="<?php echo $MATERIALLIST["Material"];?>"><?php echo $MATERIALLIST["Material"];?></option>
<?php
}
?>
</select>

</div>

</td>
<td>
<div class="MaterialDescription">
<select name="MaterialDescription" id="DesList">
<option value=""></option>
</select>
</div>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script>
function getId(val){
    $.ajax({
        type:"POST",
        url:"getdata1.php",
        data:"Material="+val,
        success: function(data){
            $("#DesList").html(data);
            }
    });

}
</script>


 </td>
<td><input type="text" name="dquantity_name" /> </td>
<td><input type="text" name="duom_name" /> </td>
</tr>

</table>


<legend> </legend>
<p> <button type="submit" class="pure-button pure-button-primary">Send Stock</button>

<br>
<br>
<?php
  echo "Distributor Page";
?>

</body>
</html>
下面的

是getdata1.php

<?php
include_once "dbConnect.php";

if(!empty($_POST["Material"])){
$Material= $_POST["Material"];
$query = "SELECT * FROM MATERIALLIST WHERE Material = $Material";
$results = mysqli_query($conn,$query);

foreach($results as $MaterialDescription){
?>
<option value="<?php echo $Des["Material"];?>"><?php echo $materialDescription ["MaterialDescription"];?></option>
<?php
}
}
?>

我可以选择材料,但在选择材料时,不会自动填充材料说明。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

PHP区分大小写。注意命名变量。 foreach包含大写变量,而echo - 小写。同样在echo中你有变量之后和开括号之前的空格。

$MaterialDescription
$materialDescription_["MaterialDescription"]