Press jquery + send formless data - bookmark

I am working on the bookmark function when the user clicks the jQueryui button and certain information is sent to the database. But I do not use the form because there is no user login information.

I pull the user id from the session data and I send the URI segment (part of the URL)

Using codeigniter / php.

I’m trying to understand what to put ajax / post functions in the data part, since there are no forms entered / no data and what to do with the controller β€œsend” part.

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"); } }); }); 
+7
source share
3 answers

Your problem is that you are checking the submit key in POST arguments. You can either fake it by sending data: {submit:true} , or by deleting your if statement and just processing the 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"); } }); }); 
+8
source

Instead of using .ajax() use .get() or .post()

Using .get()

  $.get('controller/addBookmark',function(data){ alert('Your bookmark has been saved'); }); 

Using .post()

  $.post('controller/addBookmark', function(data) { alert('Your bookmark has been saved, The contents of the page were:' + data); }); 
0
source

from jQuery.ajax () documentation

Data to send to the server. It is converted to a query string, if not already a string. It is added to the URL for GET requests.

If you do not know what to put for data, perhaps you should remove this option? In the addBookmark () controller function, you can reduce the code by removing the if mark. Try it and see if it works for you.

0
source

All Articles