【发布时间】:2020-09-17 07:40:13
【问题描述】:
我正在尝试通过 ajax 检查数据库中是否存在标题,但认为 ajax 调用不起作用。请帮我提出一些建议。
可见
<input type="text" class="form-control" id="cs_title" name="cs_name">
<span id="title_result"></span>
<script>
$(document).ready(function()
{
$("#cs_title").change(function(){
var title = $("#cs_title").val();
if (title != '') {
$.ajax({
url: "<?php echo base_url(); ?>back_end/check_title",
method:"POST",
data:{title:title},
success:function(data) {
$('#title_result').html(data);
}
});
}
});
});
</script>
在控制器中
public function check_title()
{
if ($this->cs_model->check_title($_POST['title'])) {
echo '<label class="text-danger"><span class="glyphicon glyphicon-remove"></span> Title Already Exist</label>';
}
else {
echo '<label class="text-success"><span class="glyphicon glyphicon-ok"></span> Title Available</label>';
}
}
在模型中
public function check_title($title)
{
$this->db->where('cs_title',$title);
$title_count = $this->db->get('case_study')->num_rows();
if ($title_count > 0) {
return true;
}
else {
return false;
}
}
【问题讨论】:
标签: jquery mysql ajax codeigniter-3