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

    });
1
  • @wallyk The model (bookmarks_model) is connected to the controller by the last line of the controller function. Commented May 2, 2012 at 1:35

3 Answers 3

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

});
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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!) :)
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); 
 });

6 Comments

Thanks, I'll give it a try now. What should be in the "data" place? Just the word data?
data is the results of the page on the callback.
Okay; how do I indicate data in the function? I don't have any inputs
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.
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.
|
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.

3 Comments

@chowwy check your code $this->bookmarks_model->postBookmark($id, $bookmark); in your controller and function postBookmark() { in your model, is the call made correctly?
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.