【发布时间】:2020-03-22 17:34:03
【问题描述】:
从文本框中正确获取数据后,我需要在文本框中获取并显示 id。
我需要从文本框中查看其他表格字段数据
这是我的视图和 Ajax
<div class="form-group">
<h5>Class Code:</h5>
<small><li>Only teachers shall give you code</li></small><br>
<input type="text" id="code" class="form-control col-md-6" placeholder="Enter class code to join" name="code" required />
<span id="code_result"></span>
<input type="text" name="id" id="id" class="form-control col-md-2" />
</div>
Ajax - 所以我需要获取 id 但对于我的代码,它不显示
//Verify
$('#code').on("change keyup paste", function(){
var code = $('#code').val();
if(code != '')
{
$.ajax({
url:"<?php echo base_url(); ?>students/check_code_avalibility",
method:"POST",
data:{code:code},
success:function(data){
$('#code_result').html(data);
$('input[name=id]').val(data.id); // I need to fetch this data
}
});
}
});
Controller - 这里我验证数据是否正确
function check_code_avalibility()
{
if(!$this->student_model->is_code_available($_POST["code"]))
{
echo '<label class="text-danger"><span class="glyphicon glyphicon-remove"></span> Invalid Code</label>';
}
else
{
echo '<label class="text-success"><span class="glyphicon glyphicon-ok"></span> Code Available </label><hr>';
echo '<button type="submit" class="btn btn-primary btn-md">Join to Class</button>';
}
}
型号
function is_code_available($code)
{
$this->db->where('code', $code);
$query = $this->db->get("groups");
if($query->num_rows() > 0)
{
return true;
}
else
{
return false;
}
}
【问题讨论】:
-
您似乎是复制粘贴解决方案,但没有试图理解。你的
data是一个字符串,怎么会有id的值? -
@AnuragSrivastava 我已经对此进行了修改并清楚地理解了它。你能提供一个输出吗?谢谢。
-
您对
data.id有什么期望? -
那么在您的代码中,从模型中检索 id 的查询在哪里?
-
@AnuragSrivastava 查询是什么意思?是这个
$this->db->where('code', $code); $query = $this->db->get("groups");
标签: php jquery ajax codeigniter codeigniter-3