treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Need Some Advanced jQuery/PHP help...Please?

  • Essentially, what i am trying to do is upgrade an old app i created to use some of the great AJAX features of jquery. Example: instead of limiting the admin to creating categories one at a time which can obviously be really monotonous, I would like to convert the page so that you can dynamically add a field so you can add multiple categories at once if desired...my issue now is, how do i go about properly posting the data via php? Here is what i am working with:

    http://jsfiddle.net/pseud0cool/WVzH4/

    Keep in mind i am planning on using AJAX to post the data...Let me know if you have any ideas for improving or anything that can help at all.

    Thanks Guys!
  • First of all,
    var cats = $("#categoryTable tr").length;

    won't work because if you remove one (not the last) and then add one, you will have duplicate Id's and names... Maybe you should just do a cats++ every time you add a cat.
    Secondly, Itterate through your inputs and bulid your POST data like:

    var postData = '?';

    $("#categoryTable input[type='text']").each(
    function(i,e){

    postData+= (postData!='?'?'&':'')+$(e).attr('id')+'='+$(e).val();

    });


    ( I think)
    I'm sure there are better ways though...
  • damn, i tried and tried changing it as you suggested, but still can't figure it out...
  • Yah, this is something I've been puzzling over.

    I haven't got to this point in my project yet, but I'll have to do it sometime...

    I need a solution on this as well.
  • I just don't understand how i can get the form to generate them properly, this blows! I may have to take the to stack if no one is able to help me here :(
  • instead of dynamically generating a new name for each category (category_2, category_3, category_4, etc..), why not just make "category" an array?
    -- never worry about duplicate names.
    -- _much_ easier to manage server-side.
    -- if you don't need the ids (it doesn't seem like you do), just omit them, and you won't need to do cats++ or anything, you could just clone and append and be done with it.

    as a side note, .live() is depreciated. .on() is currently the preferred method to attach event handlers (or, use .delegate() with older versions of jQuery).
  • Thanks for the thought, I will have to find something that teaches that...I pieced together some code to pull this together...I do know that .live() is deprecated, I am trying to go one step at a time so i can at least get it functioning while i am learning. Thanks for your input!
  • teaches what?

    I simply meant <input name="category[]">. Most of the adjustments you'd need to make would be removing code, not figuring out new code.
  • OH i see...duh, ok cool thanks alot...
  • I'd aggre with @traq for the array thing... You'll have to worry for one thing less...
    anyway, in the code I suggested, you are supposed to use the variable postData to your AJAX request like this

    $.ajax({
    type: "POST",
    url: "phpPage.php",
    data: postData
    })

    then, at the phpPage.php you'll handle your POST data...
  • So I understand that this would generate every field as a element in an array that I can parse with the POST data?

    I didn't know you could do that. Cool. Thanks a bunch.
  • obviously, this won't work on jsfiddle, but give it a try locally:

    http://jsfiddle.net/XHe4x/6/
  • Wow thanks! lol, though i usually like to learn by doing that at least puts me where i need to be, I am trying to learn some more advanced jQuery and this is a great start.

    Thanks again, far more than i expected!
  • no problem - and if you like to "learn by doing," great! "Do" compare and see what I did differently. If you want to "do" more, then try creating the new <input>s via javascript, instead of creating-and-hiding-and-cloning a "real" one in your HTML markup (if you notice, you'll always have an extra, empty $_POST variable with the current approach).

    and if you don't already, visit api.jquery.com - a lot of what I did is straight from the examples there.
  • That is where i am learning from for the more advanced stuff...essentially i learned PHP from the documentation online...Cheers again!