嗨,我正在使用 codeigniter,我想制作相关输入文本,如下例所示:
我有三个字段 numberphone、Name、Society,当用户输入 numberphone 时,我想自动填写 Name 和 Society:
这是我在 Mysql 中的表的结构:
人(身份证、电话号码、姓名、社会)
S 的形式如下:这是我的观点:
风景
<table>
<tr>
<td valign="top">Telephone<span style="color:red;">*</span></td>
<td>
<input type="text" name="telephone" class="text" id="telephone"/>
<?php echo form_error('telephone'); ?>
</td>
</tr>
<tr>
<td valign="top">Usager<span style="color:red;">*</span></td>
<td>
<input type="text" name="usager" class="text" id="usager" readonly="readonly">
<?php echo form_error('nbcas'); ?>
</td>
</tr>
<tr>
<td valign="top">society<span style="color:red;">*</span></td>
<td>
<input type="text" name="society" class="text" id="society" readonly="readonly">
<?php echo form_error('nbcas'); ?>
</td>
</tr>
<td><input type="submit" value="Enregistrer"/></td>
</tr>
</table>
我的控制器:
public function GetUsager()
{
if($this->input->is_ajax_request())
{
$telephone = $this->input->post('phone');
$data['row'] = $this->structure->GetUsagerId($telephone);
header('Content-Type: application/json',TRUE);
//echo(json_encode($this->structure->GetUsagerId($usager)));
// echo json_encode($data['usager']);
echo json_encode($data);
}
}
和我的模特
function GetUsagerId ($telephone)
{
$this->db->select('name','society');
$this->db->where('numberphone',$telephone);
$query=$this->db->get('Person');
if($query->num_rows() > 0)
{
$row = $query->row();
return $row;
}
}
视图中的jquery代码
$('#telephone').keyup(function() {
var phone = $('#telephone').val();
var baseurl = "<?= base_url(); ?>";
$.ajax({
type : "POST",
url : baseurl+"index.php/main/GetUsager",
dataType: 'json',
data : phone,
success: function(data) {
$("#usager").append(data.row);
}
});
});
什么都没发生。