//index.php
<!DOCTYPE html>
<head>
<title>Search Products</title>
<meta charset="UTF-8">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<?php
function get_brand(){
$conn = mysqli_connect('localhost','root','','first_db');
if(!$conn){
die('Connection Failed'.mysqli_connect_error());
}
require ("dbcon.php");
$output = '';
$sql = "SELECT distinct category FROM first_table_1";
$res = mysqli_query($conn , $sql);
if(mysqli_num_rows($res)>0){
while ($row = mysqli_fetch_array($res)) {
$output .= '<option value="'.$row["category"].'">'.$row["category"].'</option>';
}
}
return $output;
}
?>
<form action='form.php' method='POST'>
<div class="container">
<div class="row">
<div class="col-lg-4"></div>
<div class="col-lg-4">
<div class="form-group">
<label for="brand">Category</label>
<select name="brand" id="brand" class="form-control">
<option value="">Select category</option>
<?php echo get_brand(); ?>
</select>
</div>
<br><br>
<div class="form-group">
<label for="item">Goods</label>
<select name="item" id="item" class="form-control">
<option value="">Select goods</option>
</select>
</div>
</div>
<div class="col-lg-4"></div>
</div>
</div>
<input type='submit' name='Send' value='send'>
</form>
<script type="text/javascript">
$(document).ready(function(){
$('#brand').change(function(){
var category = $(this).val();
$.ajax({
url: "fetch_model.php",
method: "POST",
data: {category:category},
success:function(data)
{
$('#item').html(data);
}
});
});
});
</script>
</body>
</html>
//fetch_model.php
<?php
require ("dbcon.php");
$output = '';
$sql = "SELECT distinct goodsname FROM first_table_1 WHERE category = '".$_POST["category"]."' ";
$res = mysqli_query($conn , $sql);
$output .= '<option value="" disabled selected>Select goods</option>';
if(mysqli_num_rows($res)>0){
while ($row = mysqli_fetch_array($res)) {
$output .= '<option value="'.$row["id"].'">'.$row["goodsname"].'</option>';
}
}
echo $output;
?>
//form.php
<?php
$goodsname = $_POST['item'];
$select = htmlspecialchars ($_POST["item"]);
$dbc = mysqli_connect('localhost', 'root', '', 'first_db') or die ('Error');
echo "goods= ". $select. "<br>";
$query = "SELECT price, count FROM first_table_1 WHERE goodsname = '$goodsname'";
$result = mysqli_query($dbc, $query) or die ('Error');
echo $query."<br>";
if($result)
{
$rows = mysqli_num_rows($result);
echo "<table border='3'><tr></tr>";
for ($i = 0 ; $i < $rows ; ++$i)
{
$row = mysqli_fetch_row($result);
echo "<tr>";
for ($j = 0 ; $j < 2 ; ++$j) echo "<td>$row[$j]</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
}
?>
При нажатии на кнопку submit файл index.php обработка происходит у form.php но почему-то с select name = 'item' передается пустое значение. в чом проблема?
console.log(data);
- ?console.log(data);
первой строчкой в функциюsuccess:function(data) {
. Что выводится в консоли браузера?