到目前为止我一直在学习,但是这种 db ajax 通信总是让我头疼不已。
这是控制器:
function become_teacher()
{
$this->load->model('class_model');
$data = array('user_id' => $this->input->post('user_id'),
'group_id' => $this->input->post('user_group')
);
$this->class_model->change_to_teacher($data);
}
这是模型:
function change_to_teacher($data)
{
$this->db->insert('user_groups', $data);
return;
}
这是脚本:
<p>Om du vill kunna skapa lektionen bli lärare. </p>
<button class="btn teacher">Bli lärare</button>
<script type="text/javascript">
var user_id = $('.meta').text().trim();
var user_group = '4';
$('.teacher').on('click', function() {
$.ajax({
type: "POST",
url: "http://localhost:8888/testing/index.php/cours/become_teacher",
data: "{ user_id: user_id, user_group: user_group }",
contentType: "application/json; charset=utf-8",
dataType: "json",
});
});
</script>
var user_id
正在从头文件中获取其数据,此处未包含。任何帮助将非常感激!