1

I'm working on a javascript function that when called will make a ajax request to a separate page. The separate page handles manipulating and displaying an array.

To display the list I use the function:

$('#pub-mod [name="userid"]').change(function() {
    var usr = $('#pub-mod [name="userid"]').val();
    if (usr != '') {
        $.ajax({
        url : 'ajax_admin_load_pubs.php',
        data: {'action' : 'load_'+usr},
        dataType : "text",
        success : function(response) {  
            $('#pub-mod-list').html(response);
        }
        });
    }
});

This function is working as intended, and if I place print_r($_GET); in the ajax file I have Array ( [action] => load_userid ) show up.

The function to edit the list looks like this:

function modify_pubs(action) {
    $.ajax({
        url : 'ajax_admin_load_pubs.php'
        data: {'action' : action},
        dataType : "text",
        success : function(response) {  
            $('#pub-mod-list').html(response);
        }
    });
}

This function is not working as intended, and if I place print_r($_GET); in the ajax file I have Array ( [action] => load_load ) show up.

I've verified that action in the second function is returning the correct value, and at this point I'm kind of at a lost as to what could be wrong.

Any ideas?

Answers to comments thus far:

I'm calling modify_pubs from text links generated by the code used to display it. (i.e. <button onclick="modify_pubs(userid_c_up);">up</button>

The portion for "modify_pubs(userid_c_up)" was checked by just having it alert the value being passed.

Also, I expect to see Array ([action] => text_string_from_action) – Charles Smith 3 mins ago edit

Action is a text string that is underscore delimited to run separate portions of the ajax code.

5
  • How are you calling the modify_pubs function? Commented Oct 31, 2011 at 16:28
  • What do you expect to see? From the code shown, you're getting what you should be. Commented Oct 31, 2011 at 16:28
  • What is action ? Where is it defined ? Commented Oct 31, 2011 at 16:33
  • I'm calling modify_pubs from text links generated by the code used to display it. (i.e. <button onclick="modify_pubs(userid_c_up);">up</button> The portion for "modify_pubs(userid_c_up)" was checked by just having it alert the value being passed. Also, I expect to see Array ([action] => text_string_from_action) Commented Oct 31, 2011 at 16:33
  • @mdi Action is a text string that is underscore delimited to run separate portions of the ajax code. Commented Oct 31, 2011 at 16:35

1 Answer 1

1

First of all, why not use POST instead of GET?

And second, i do not know if this will help, but you are doing it wrong.

According to the jQuery docs, you put the the location where you want your request to be sent into the "url", and the data you want to send in "data", you do not combine the 2 in the url, so maybe this will work. (Using .get() instead of .ajax())

$.post("ajax_admin_load_pubs.php", { "action": action},
   function(response){
       $('#pub-mod-list').html(response);
   }, "text");

And with just a little edit of your example:

function modify_pubs(action) {
    $.ajax({
        url : 'ajax_admin_load_pubs.php',
        data : {"action" : action},
        dataType : "text",
        type : 'post',
        success : function(response) {  
            $('#pub-mod-list').html(response);
        }
    });
}
Sign up to request clarification or add additional context in comments.

5 Comments

Went ahead and tried this, and didn't fix the issue, you're correct though, so I'll update the original post to reflect this.
Also, why not use POST? I would use post, because then there is lesser risk that my data get reformatted in some strange way.
Changing the ajax call to have type : 'post', fixed it. If you can edit your answer I'll accept it. Also, on data you should have the array key in quotes. Thanks
Changed the answer to reflect the answer given in previous comment.
just noticed you didn't add type : 'post', this is required for jQuery.ajax because the default is type : 'get',

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.