I've a php page where there is a ajax concatenate select and a div with the total number of records in the table tb1
HTML
<p>
<select name="model" id="model">
<?php echo $myclass->selModel(); ?>
</select>
</p>
<br />
<p>
<select name="year" id="year">
<option value=""> </option>
</select>
</p>
<p>
<div id="total"><?php echo $myclass->recordNum();?></div>
</p>
JS
// VARS
var wait = '<option>Waiting</option>';
var select = '<option value="">Select</option>';
// CAT_1 //
$("select#model").change(function(){
var model = $("select#model option:selected").prop("value");
$("select#year").html(wait);
$.post("ajax-php/select-concat.php", {model:model}, function(data){
switch (data) {
case select:
$("select#year").html("");
return false;
default:
$("select#year").prop("disabled", false);
$("select#year").html(data);
return false;
}
});
});
PHP - select-concat.php
if(isset($_POST['model']))
{
echo $myclass->selYear();
die;
}
MY PHP CLASS
public function selYear() {
$result = $this->db->mysqli->query
("SELECT year FROM tb1 WHERE model='$_POST[model]");
$year = '<option value="">Select</option>';
while($row = $result->fetch_assoc()) {
$year .= '<option value="' . $row['year'] . '"';
$year .= '>' . $row['year'] . '</option>';
}
return $year;
}
private function recordNum(){
$result = $this->db->mysqli->query
("SELECT * FROM tb1 WHERE something ");
$numrec = $result->num_rows;
return $numrec;
}
My goal is to update the number of records with ajax every time I run a concatenate select. How could I do that? Thanks
You should use json_encode() on select-concat.php to return two results.
if(isset($_POST['model']))
{
$years = $myclass->selYear();
$total = $myclass->recordNum();
$arr = array('years' => $years, 'total' => $total);
echo json_encode($arr);
die;
}
You must add POST model value to recordNum()
function (or pass it with an argument).
Then...
$("select#model").change(function(){
var model = $("select#model option:selected").prop("value");
$("select#year").html(wait);
$.post("ajax-php/select-concat.php", {model:model}, function(data){
var json_data = $.parseJSON(data);
switch (data) {
case select:
$("select#year").html("");
$("#total").html(0);
return false;
default:
$("select#year").prop("disabled", false);
$("select#year").html(json_data.years);
$("#total").html(json_data.total);
return false;
}
});
});