[英]CodeIgniter 3: Submit to Controller in Ajax
我正在嘗試創建通知功能,我希望ajax能夠通過按鈕單擊時的控制器運行查詢
這是我的腳本
$('#noti_Button').click(function (e) {
e.preventDefault();
$.ajax({
url: '<?php echo site_url("profile/read_notif")?>'
});
});
控制器
public function read_notif(){
$this->profile_model->read_notifs($data['id']);
return;
}
和模型
function read_notifs($id)
{
$read = array(
'read' => '1'
);
$this->db->where('recipient', $id);
$this->db->update('tbl_notifications', $read);
return;
}
我試過了,數據庫中的數據沒有更新。 在我的HTML中,它只是一個簡單的按鈕
它是用紅寶石在軌上稱ajax的樣本。 在這段代碼中,我們正在調用控制器以獲取值。
$('#Button').on('change', function(event) {
var selected_resource_id = $(this).val();
$.ajax({
type: 'GET',
url: "<%= Home_index_path %>",
data: { id: selected_resource_id },
success: function (data) {
alert("Ajax success");
}
});
});
腳本
$('#noti_Button').click(function (e) {
e.preventDefault();
$.ajax({
url: '<?php echo site_url("profile/read_notif")?>',
success:function(data)
{
alert(data);
}
});
});
控制者
public function read_notif(){
$this->profile_model->read_notifs($data['id']);
echo $this->db->last_query();
}
使用錯誤:檢查提交表單時是否有任何錯誤。
$('#button').click(function (e) {
e.preventDefault();
$.ajax({
url: '<?php echo site_url("profile/read_notif")?>',
success:function(data)
{
alert(data);
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.