SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Send Array Object to jQuery Get

    I'm trying to send my array object through ajax using the jQuery .get method, but when it sends, ids show up as multiple parameters and I'm not sure if that's the way to do it.

    Here is my code:

    Code JavaScript:
     
    var val = [];
    $(':checkbox:checked').each(function(i){
         val[i] = $(this).attr('id').substring(6);
    });
     
    $.get("/assets/ajax/pm_change_status.php", { s: sess_id(), 'ids[]': val } );

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by unemployment View Post
    I'm trying to send my array object through ajax using the jQuery .get method, but when it sends, ids show up as multiple parameters and I'm not sure if that's the way to do it.
    A standard way to send form data is to use serialize.

    Code javascript:
    $.get("/assets/ajax/pm_change_status.php", $('#myform'.serialize());
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks paul, but can't I just use this?

    $.get("/assets/ajax/pm_delete_messages.php", { s: sess_id(), ids: JSON.stringify(val) } );

    It seems to be working. Is this not best practice?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by unemployment View Post
    Thanks paul, but can't I just use this?

    $.get("/assets/ajax/pm_delete_messages.php", { s: sess_id(), ids: JSON.stringify(val) } );

    It seems to be working. Is this not best practice?
    It's better to use the form to maintain the data, but what you're doing seems to be working.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I'm not actually submitting a form though. All I am doing is sending the ID to ajax to do the form like manipulation. One of the reasons I am doing this is so that I can have my buttons styled consistent across my site. Style links and buttons the same leads to a lot of styling conflicts which is why I'm making everything links and not using forms.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •