Treehouse: Grow your CSS skills. Land your dream job.

Need Some Advanced jQuery/PHP help…Please?

  • # April 5, 2012 at 2:16 am

    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:

    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!

    # April 5, 2012 at 2:42 am

    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(

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


    ( I think)
    I’m sure there are better ways though…

    # April 5, 2012 at 3:44 pm

    damn, i tried and tried changing it as you suggested, but still can’t figure it out…

    # April 5, 2012 at 9:18 pm

    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.

    # April 5, 2012 at 10:14 pm

    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 :(

    # April 5, 2012 at 11:51 pm

    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).

    # April 6, 2012 at 1:04 am

    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!

    # April 6, 2012 at 1:18 am

    teaches what?

    I simply meant . Most of the adjustments you’d need to make would be removing code, not figuring out new code.

    # April 6, 2012 at 1:55 am

    OH i see…duh, ok cool thanks alot…

    # April 6, 2012 at 3:23 am

    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

    type: "POST",
    url: "phpPage.php",
    data: postData

    then, at the phpPage.php you’ll handle your POST data…

    # April 6, 2012 at 2:41 pm

    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.

    # April 6, 2012 at 10:16 pm

    obviously, this won’t work on jsfiddle, but give it a try locally:

    # April 7, 2012 at 2:02 am

    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!

    # April 7, 2012 at 2:12 am

    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 – a lot of what I did is straight from the examples there.

    # April 7, 2012 at 5:59 am

    That is where i am learning from for the more advanced stuff…essentially i learned PHP from the documentation online…Cheers again!

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic.