0

I'm trying to create a interface with a conditional logic that will allow the webmaster to insert advertisments anywhere on the site. But before coding it, I need some advices from you :)

The forms are handled trough jquery & ajax. When the "add another ad" button is pressed a new form is created with jQuery. My question is how could I gather all the form input values into a single hidden input that would look like:

<input name="ad[]" type="hidden" value="homepage,after_article,4,visitors" />

I think this value would be easier for me to handle in PHP.

enter image description here

3
  • mm.. wouldnt you need the key of the field with the value? i mean, instead of homepage,after_article,4,visitors wouldnt be better foo:homepage,bar:after_article,n:4,baz:visitors? Commented Jan 29, 2011 at 17:44
  • yes, it would probably be a good idea to include the key too. How do I do that? Commented Jan 29, 2011 at 17:47
  • you might find code review useful. Commented Jan 30, 2011 at 10:36

2 Answers 2

2

You could use the following to gather all the values:

var values = $('#yourformId :input').map(function() {
    return $(this).val();
}).toArray().join(',');

and then set the value of your hidden field:

$('#ad').val(values);

And you can see it in action here.

Sign up to request clarification or add additional context in comments.

Comments

1

You could also do:

var values = $('#yourformId').serialize();
$('#ad').val(values);

jQuery's form.serialize function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.