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");
}
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With