简体   繁体   中英

how to get values from multiple select/listboxes in php?

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>";

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'] );

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM