Php Ajax连接选择和总计记录查询
我有一个php页面,其中有一个ajax连接select和一个div,表tb1中有记录的总数 HTML 我的PHP课程Php Ajax连接选择和总计记录查询,php,jquery,Php,Jquery,我有一个php页面,其中有一个ajax连接select和一个div,表tb1中有记录的总数 HTML 我的PHP课程 public function selYear() { $result = $this->db->mysqli->query ("SELECT year FROM tb1 WHERE model='$_POST[model]"); $year = '<option value="">Select</
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;
}
我的目标是在每次运行连接选择时使用ajax更新记录数。我怎么能这么做?谢谢您应该在select-concat.php上使用json_encode返回两个结果
if(isset($_POST['model']))
{
$years = $myclass->selYear();
$total = $myclass->recordNum();
$arr = array('years' => $years, 'total' => $total);
echo json_encode($arr);
die;
}
您必须将POST模型值添加到recordNum函数,或使用参数传递它
然后
$("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;
}
});
});
是的,我知道,但是我想通过友好方式更新ajax记录编号。在选择更改时,我只重新加载SelectField,我还想更新记录编号。
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;
}
if(isset($_POST['model']))
{
$years = $myclass->selYear();
$total = $myclass->recordNum();
$arr = array('years' => $years, 'total' => $total);
echo json_encode($arr);
die;
}
$("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;
}
});
});