10

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");
                }
            });  

    });
chowwy
  • 1,126
  • 8
  • 26
  • 45
  • @wallyk The model (bookmarks_model) is connected to the controller by the last line of the controller function. – chowwy May 02 '12 at 01:35

3 Answers3

8

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");
            }
        });  

});
Brombomb
  • 6,988
  • 4
  • 38
  • 57
  • I tried this with data: {submit:true} and without data. I also tried it without the if function. I'm getting the alert, but nothing is being added to the database. – chowwy May 02 '12 at 01:49
  • Okay, I did some maneuvering with my buttons and it is now working--with nothing in the data field as you suggested. Instead, I added a hidden field to get the data to pass. I've accepted and upvoted your answer, since it was the closest. (Now if I can just get the alert to work!) :) – chowwy May 02 '12 at 03:59
0

Instead of using .ajax() use either .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); 
 });
Control Freak
  • 12,965
  • 30
  • 94
  • 145
  • Thanks, I'll give it a try now. What should be in the "data" place? Just the word data? – chowwy May 02 '12 at 01:35
  • `data` is the results of the page on the callback. – Control Freak May 02 '12 at 01:36
  • Okay; how do I indicate data in the function? I don't have any inputs – chowwy May 02 '12 at 01:37
  • The `data` variable is an output of the contents of the submitted page, in case you want to return it in your code , not an input.. See the example of how it's used under the `.post()` example. – Control Freak May 02 '12 at 01:37
  • Okay, I tried both functions. The first produced an alert, but nothing is in the database. The second gave me an alert, but it contained an exception error; also nothing in the database. Any other thoughts? I'm really stumped with this. – chowwy May 02 '12 at 01:55
  • Unfortunately i don't know PHP, so it's something in your PHP code that isn't setup properly.. This simply sends a request to the page with no further post data, as your question states. If you were saying the 2nd one gave you an error, then that means your php page is returning an error and the `data` variable is catching it from the php page and putting it into the alert box. You may need to debug your `controller/addBookmark` page. – Control Freak May 02 '12 at 01:57
0

from the documentation of jQuery.ajax()

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests.

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

Jasonw
  • 5,054
  • 7
  • 43
  • 48
  • @chowwy check your code `$this->bookmarks_model->postBookmark($id, $bookmark);` in your controller and `function postBookmark() {` in your model, is the call made correctly? – Jasonw May 02 '12 at 02:13
  • The variable `$user_id` and `$bookmark` in function is a local variable to function `postBookmark()`. The function `addBookmark()` call `bookmarks_model->postBookmark($id, $bookmark);` with **two** parameters. So you are passing two parameters to function postBookmark but in the function `postBookmark`, it expect no parameter. So really, how is the insertion into database is done? – Jasonw May 02 '12 at 02:45
  • That was just an oversight when I transferred the code. I have the function working fine without jQuery. But I'd like to post the information to the database using an ajax/post function. – chowwy May 02 '12 at 03:52