我正在一个书签功能,其中用户点击一个按钮jQueryUI的和某些信息被发送到数据库。但我不使用的一种形式,因为没有信息的用户进入。
我是从会话数据拉动用户的ID,而我送一个URI段(URL的一部分)
使用codeigniter / PHP。
我试图找出放于阿贾克斯/后函数的数据部分是什么,因为没有表/无输入的数据,以及如何处理控制器的提交的一部分。
控制器
函数收藏本站(){
如果($这个 - >输入 - >后期('提交')){
的$ id = $这个 - >会话级>用户数据('身份证');
$书签= $这 - > URI的>段(3,0);
$这个 - > bookmarks_model-> postBookmark($ ID,$书签);
}
}
型号
函数postBookmark(){
$数据=阵列(
USER_ID'=> $ user_ID的,
bookmark_id'=> $书签,
);
$这个 - > DB->插入('书签',$数据);
}
HTML
<按钮类=somebutton>添加书签及LT; /按钮>
的jQuery
$('。somebutton)。点击(函数(){
$阿贾克斯({
网址:控制器/收藏本站',
键入:POST,
数据: ???,
成功:函数(结果){
警报(您的书签已被保存);
}
});
});
解决方案
您的问题是要检查的提交
在发表的args。您可以伪造它通过发送
数据:{提交:真}
或通过删除,如果你的发言,只是处理POST请求
$('。somebutton)。点击(函数(){
$阿贾克斯({
网址:控制器/收藏本站',
键入:POST,
数据:{提交:真} //用钥匙'提交'和值'真实的对象;
成功:函数(结果){
警报(您的书签已被保存);
}
});
});
I'm working on a bookmarking function where the user clicks on a jQueryui button and certain information is sent to the database. But I'm not using a form, because there is no information for the user to enter.
I'm pulling the user's ID from the session data, and I'm sending a URI segment (portion of the URL)
Using codeigniter/php.
I'm trying to figure out what to put in the data portion of the ajax/post function, since there's no form/no data entered, and what to do about the "submit" part of the controller.
Controller
function addBookmark(){
if ($this->input->post('submit')) {
$id = $this->session->userdata('id');
$bookmark = $this->uri->segment(3, 0);
$this->bookmarks_model->postBookmark($id, $bookmark);
}
}
Model
function postBookmark() {
$data = array(
'user_id' => $user_id,
'bookmark_id' => $bookmark,
);
$this->db->insert('bookmarks', $data);
}
HTML
<button class="somebutton">Add bookmark</button>
jQuery
$('.somebutton').click(function() {
$.ajax({
url: 'controller/addBookmark',
type: 'POST',
data: ???,
success: function (result) {
alert("Your bookmark has been saved");
}
});
});
解决方案
Your problem is you are checking for a submit
key in the POST
args. You can either fake it by sending data: {submit:true}
or by by removing your if statement and just processing a POST request
$('.somebutton').click(function() {
$.ajax({
url: 'controller/addBookmark',
type: 'POST',
data: {'submit':true}, // An object with the key 'submit' and value 'true;
success: function (result) {
alert("Your bookmark has been saved");
}
});
});