Jump to content

Recommended Posts

Hi Guys,

 

This question is actually part PHP, Part MySQL and part AJAX so I hope I'm ok posting it in here, if not feel free to move it to its rightful home.

 

Bascially, I've built an e-commerce site and the client wants the option to create product sets, whereby he can add a selection of products together to form one 'meta product'.

 

I figured the best way of doing this would be to create an array called 'setItems[]' and the form to create new instances of 'setItems[]' whenever the user clicks on an 'add new product' link.

 

This would then add item ID's to the array which will then be inserted into the DB... I presume this is the right way of doing it, the one place I'm kinda stuck on is the AJAX'y part, I've no idea on how to make the 'add new product' link create another form entry. I've used JQuery exactly once and even then it was basically cut'n'paste.

Ok I've made some progress using jQuery but it is inserting the field at the bottom of the page rather than below the previous element like I want it to do. Here is the code:

 

 function addAnother() {  
jQuery('#addSetForm').append('<input type="text" name="setItems[]" id="setItems[]" />');
}

Merdok,

There's nothing wrong with the jQuery. I would probably just check what #addSetForm element is and make sure that is the element you want to add the input. If it's adding to the bottom of the page, you probably don't want to add to #addSetForm but something else. In any case, please know that you should have only one ID per page so you'll have to change id="setItems[]" to something else. You can use a class like class="setItems" but IDs should be unique so if you have more than one on the same page, it's not good.

Ah! :)

 

Yeah that makes sense, it was appending the box to the end of the form, I added a named div around the box and it worked.

 

Re: the form ID, I actually know that, clearly just a moment of stupidity, Thanks for pointing it out. :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.