Alby
Alby

Reputation: 17

Php form select with multiple choices

the following code shows a "form" page (html) where people can select an option (read from option.txt file) sent to process_data.php option.txt file is like this:

 option1|this is the option 1
 option2|this is the option 2
 .....

Everything works fine: in process_data.php when i call $_post['mydecision'] the variable is correctly read ($item[0]). The issue is born when I want to use also $item[1] relating to the same option selected by the user. The code:

 <input type="hidden" name="mydecision2" id="mydecision2" value="<?php echo "$item[1]"; ?>">

gives always the same result: $_post['mydecision2'] = "this is the option 1" even though users select different options.

Any suggestions?

<body>
<form name="web_form" id="web_form" method="post" action="process_data.php">
<select name="mydecision">
<option selected="selected">SELECT DECISION</option>
<?php
// define file
$file = 'options.txt';
$handle = @fopen($file, 'r');
if ($handle) {
while (!feof($handle)) {
   $line = fgets($handle, 4096);
   $item = explode('|', $line);
   echo '<option value="' . $item[0] . '">' . $item[1] . '</option>' . "\n";
 }
 fclose($handle);
 }
 ?>
 </select>

 <input type="hidden" name="mydecision2" id="mydecision2" value="<?php echo "$item[1]"; ?>">
 ....

In a few words: how can i take both values chosen? I need also the $item[1] (correspondent to the chosen $item[0]) as variable $_post['mydecision2'] to be available in process_data.php

Upvotes: 1

Views: 2593

Answers (1)

Abhinav
Abhinav

Reputation: 8168

Your select should be like this:

<select name="mydecision[]" multiple>

Upvotes: 1

Related Questions