Dew Drop
Dew Drop

Reputation: 71

Posting corresponding datas on clicking submit button

for($i=0;$i<2;$i++)
{
?>
<tr>
<td>
<select name="sel_<php echo $i; ?>" id="sel_<php echo $i; ?>">
<option value="1">A</option>
<option value="2">B</option>
</select>
</td>

<td>
<input type="submit" name="sub_<?php echo $i; ?>" value="submit" />
</td>
</tr>
<?php
}
?>

Now I have 2 select boxes sel_1 and sel_2, each of them have corresponding submit button sub_1 and sub_2. I want to post the corresponding data when a submit button is pressed. For example: when I press sub_1 I need to post the value of sel_1.

So, if press on the submit button in a specific row, I need to post the value of the select box in that row.

How can I do this?

Upvotes: 0

Views: 37

Answers (2)

Sourabh Kumar Sharma
Sourabh Kumar Sharma

Reputation: 2827

since you have not mentioned the form method i am using $_REQUEST here,

if (isset($_REQUEST['sub_0'])) {
   // use the value of $_REQUEST['sel_0'];
} elseif (isset($_REQUEST['sub_1'])) {
    // use the value of  $_REQUEST['sel_1'];
}

Note: you can replace $_REQUEST with $_POST or $_GET as per you form method type or else you can use $_REQUEST itself if you wish to do so

Upvotes: 0

pavel
pavel

Reputation: 27102

It doesn't matter that both select are sent. Just work with the one select you need.

if (isset($_POST['sub_0'])) {
    $data = $_POST['sel_0'];
} elseif (isset($_POST['sub_1'])) {
    $data = $_POST['sel_1'];
}

In $data will be value of the first, or the second one, select.

IF you need to send just one selectbox, you need more forms (each form will contain select and submit).

Upvotes: 1

Related Questions