0

Everybody: I need to send data from 20 drop downs to a PHP file. How can I make some loop over them to see values to all of them? Thanks you.

P.S.: In PHP I know how to walk through them to get the data...

5
  • Can you please be more specific? Commented Mar 18, 2012 at 18:01
  • I've got table with 2 cols and about 10 rows but user can add or remove rows.In every <td> I've got drop down.in every col drop downs are the same.I need to see what values are selected in all elements in first col then in second and send them vie ajax to php file to be saved in DB. Commented Mar 18, 2012 at 18:09
  • @stackoverflow - a very official edit made by a very official user ;) Commented Mar 18, 2012 at 18:19
  • @Lix - What is your point honey? Or did you just need some activity to do, so you can finally get Mortarboard badge? You made it. Congratulation :) Commented Mar 18, 2012 at 21:39
  • @stackoverflow, No point :) There was a discussion recently mentioning your username - meta.stackexchange.com/questions/125665/… Commented Mar 18, 2012 at 22:20

2 Answers 2

1

If you need to create an array of values you should definitely check the serializeArray method. You will find an excellent usage example on the same page is you scroll down a little bit.

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

Comments

0

Let us take this HTML as a sample -

<div id="vanyov">
  <select>
    <option value="Beer"></option>
    <option value="Wine"></option>
    <option value="Water"></option>
    ...
  </select>
  <select>
    <option value="Steak"></option>
    <option value="Pizza"></option>
    ...
  </select>
  ...
</div>
<!-- I hope I put the right choices in here.
everyone cool with Beer and Pizza right? ;) -->

You could use jQuery to return all the select elements you need and then parse though all of them adding them to a JSON object.

var valuesJSON = {};
$.each($("#vanyov select"),function(index,elem){
  valuesJSON[index] = $(elem).val();
});

This would add the values of all the <select> elements with in the #vanyov element.

But what's going on here? Lets take a look -

  • var valuesJSON = {} - This is some shorthand code to create a new empty object named valuesJSON.
  • $.each - This is a jQuery function that will loop though all the elements you give it. Arrays and Objects and especially groups of elements that jQuery selectors return. There is also a callback function that is passed each of the items that $.each found.
  • $("#vanyov select") - Here we are telling jQuery to give us all of the <select> elements that are descendants of the #vanyov element.
  • valuesJSON[index] - This is where we are adding a new item to the JSON. We use the index parameter because they are right there! Easy! The $.each function provides them.
  • $(elem).val() - We wrap the elem parameter with $() to convert it into a jQuery object and then we use the val() function to extract the value of the <option> element within the dropdown that was selected.

Then send out your order with an Ajax call perhaps -

$.post('/ajax/lyubomir',valuesJSON,function(response){
  // Return values here
},"json");

I hope they come soon - After that, I'm starving...

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.