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:
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:
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!
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
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.
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.
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!
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:
( I think)
I'm sure there are better ways though...
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.
-- 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 docats++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)..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!I simply meant
<input name="category[]">. Most of the adjustments you'd need to make would be removing code, not figuring out new code.anyway, in the code I suggested, you are supposed to use the variable postData to your AJAX request like this
then, at the phpPage.php you'll handle your POST data...
I didn't know you could do that. Cool. Thanks a bunch.
http://jsfiddle.net/XHe4x/6/
Thanks again, far more than i expected!
and if you don't already, visit api.jquery.com - a lot of what I did is straight from the examples there.