-1
Hello, I’m doing a project with dynamic selects, I managed to make the selects take the database information, but I wanted to create a button so when it is clicked return me the data of this query:
" SELECT * FROM tbl_localizacao_lote WHERE '". $_POST["category_id"]." ' = '". $_POST["sub_category_id"]."' ";
Being category_id the variable of the first select and sub_category_id the variable of the second. I will show Jquery below:
INDEX.PHP
<!DOCTYPE html>
<html>
<head>
<title>Dinâmico</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>
</head>
<body>
<br />
<div class="container">
<h3 align="center">Dinâmico</h3>
<br />
<div class="panel panel-default">
<div class="panel-heading">Select Data</div>
<div class="panel-body">
<div class="form-group">
<label>Select Category</label>
<select name="category_item" id="category_item" class="form-control input-lg" data-live-search="true" title="Select Category">
</select>
</div>
<div class="form-group">
<label>Select Sub Category</label>
<select name="sub_category_item" id="sub_category_item" class="form-control input-lg" data-live-search="true" title="Select Sub Category">
</select>
<input id='alo' type="submit" value="Teste">
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
$('#category_item').selectpicker();
$('#sub_category_item').selectpicker();
load_data('category_data');
function load_data(type, category_id = '') {
$.ajax({
url: 'load_data.php',
method: 'POST',
data: { type: type, category_id: category_id },
dataType: 'json',
success: function (data) {
var html = '';
for (var count = 0; count < data.length; count++) {
html +=
'<option value="' +data[count].id +'">' + data[count].name +'</option>';
}
if (type == 'category_data') {
$('#category_item').html(html);
$('#category_item').selectpicker('refresh');
} else {
$('#sub_category_item').html(html);
$('#sub_category_item').selectpicker('refresh');
}
},
});
}
$(document).on('change', '#category_item', function () {
var category_id = $('#category_item').val();
load_data('sub_category_data', category_id);
});
$(document).on('change', '#sub_category_item', function () {
var sub_category_id = $('#sub_category_item').val();
load_data('sub_category_data', sub_category_id);
});
});
</script>
</body>
</html>
LOAD_DATA.PHP
<?php
$connect = new PDO("mysql:host=localhost;dbname=testing", "root", "");
if(isset($_POST["type"]))
{
if($_POST["type"] == "category_data")
{
$query = "
SELECT DISTINCT CardName FROM tbl_localizacao_lote WHERE CardName IS NOT NULL ORDER
BY CardName ASC
";
$statement = $connect->prepare($query);
$statement->execute();
$data = $statement->fetchAll();
foreach($data as $row)
{
$output[] = array(
'id' => $row["CardName"],
'name' => $row["CardName"],
);
}
echo json_encode($output);
}
else
{
$query = "
SELECT ItmsGrpNam FROM tbl_localizacao_lote
WHERE CardName = '".$_POST["category_id"]."' GROUP BY ItmsGrpNam
ORDER BY ItemCode ASC
";
$statement = $connect->prepare($query);
$statement->execute();
$data = $statement->fetchAll();
foreach($data as $row)
{
$output[] = array(
'id' => $row["ItmsGrpNam"],
'name' => $row["ItmsGrpNam"]
);
}
echo json_encode($output);
}
}
?>
DATABASE
I can already get the values of the selects with jquery, but I don’t know how to do it when you click the button it returns me the data of the query I put in quote above, if anyone knows, I appreciate.
"but I don’t know how I would do when I click the button it returns me the query data" Have you tried making an ajax call? has several examples here and on the internet, basically would php run the query and then return the values to the javascript that will show this
– Ricardo Pontual
You just have to do one more AJAX like you did before. I don’t see what the problem is.
– BRABO
I wouldn’t need to create another php just for it to pull the data from select? I would just create one more ajax?
– AugustoRm
you can send the new ajax to the same php if you do an if and make the new ajax enter inside that if. But I advise to do another php controller, one for each AJAX is indicated, it is simpler to debug and everything is more organized
– BRABO
perfect, I’ll try to do, if there are any more problems I update here, vlw by help
– AugustoRm