0

i want to know that how to get multiple selectbox values in php means that i have more than 1 select box on a page and it can be increased when user click on addmeans that a page can contain numerous selectboxes now plz tell me how to get the values from that boxes and also how do i get know that how many select boxes were used by user

i think an array should be used but i dont know how??

    <select name="select" id="select">

<option value="1">value1</option>

<option value="2">value2</option>

<option value="3">value2</option>

</select>

    <select name="select" id="select">

<option value="1">value1</option>

<option value="2">value2</option>

<option value="3">value2</option>

</select>



    <select name="select" id="select">

<option value="1">value1</option>

<option value="2">value2</option>

<option value="3">value3</option>

</select>

like in above example how do i get know that how many selectboxes were used and how to get value of each box

code for adding the new dropdown

function addRow()
            {
                var newRow = document.all("tblGrid").insertRow();


                var oCell = newRow.insertCell();
                oCell.innerHTML = "<select name='select' id='select'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select>";
1

2 Answers 2

1

First of all you need to use unique id and name for each select box. Otherwise it is difficult to manage them after post.

So you can do it something like this:

<select name="myselect[]" id="select1">

<select name="myselect[]" id="select2">

<select name="myselect[]" id="select3">

After form post you can get all select box values:

print_r( $_POST['myselect'] );

For number of select boxes on page you can try:

echo count( $_POST['myselect'] );

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

7 Comments

Can you please show your code which is adding new select boxes.
function addRow() { } var newRow = document.all("tblGrid").insertRow(); //add 3 cells (<td>) to the new row and set the innerHTML to contain text boxes var oCell = newRow.insertCell(); oCell.innerHTML = "<select name='select' id='select'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select>";
You need to use some counter in following line to get a unique id and name: oCell.innerHTML = "<select name='select' id='select'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select>";
Try: oCell.innerHTML = "<select name='select[]' class='select'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select>";
Thanks NAVEED this works tell me 1 more thing that how we can use php values in that javasctipt code
|
1

Change the name attribute value in each tag to something unique like 'select1','select2','select3' . To refer these values in PHP use $_POST['select1'] and so on. OR use $_GET['select1'] incase of get method is used in the form.

2 Comments

one dropdown box is shown on main page while other are created when user click on add other dropdown were clone of the main dropdown
+1. no matter how many dropboxes you create, they'll be POSTed even they are contained within a <form> tag. As directed above, make them unique.

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.