Charles Smith
Charles Smith

Reputation: 284

JQuery Ajax not working as expected with PHP $_GET

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.

Upvotes: 1

Views: 529

Answers (1)

Webbies
Webbies

Reputation: 957

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

Upvotes: 1

Related Questions