How to Sends Two Values in Select Dropdown Using PHP

How to sends two Values in select dropdown using php

Place both values into the value attribute, separated by "/"

<option value="<?=$halls['id'] . '/' . $halls['rang_from'] . '/' .  $halls['rang_to']?>"><?= $halls['rang_from']?> To <?=$halls['rang_to']?>
</option>

And When if you want to Recieve It Then Write

list($hall_id, $rang_from, $range_to) = explode('/', $_REQUEST['hall']);

for Checking that values comes or Not... Write

echo $rang_from; exit;

Send multiple values from dropdown view to the controller action

You haven't kept the variable inside option's value tag.

     <select name='select'>
<option selected disabled>Choose Stations</option>
<?php foreach ($get_stations as $get_stations_item): ?>
<option value="<?php echo $get_stations_item['sourcestationid'].','.$get_stations_item['destinationstationid']; ?>">Station <?php echo $get_stations_item['sourcestationid']; ?> - Station <?php echo $get_stations_item['destinationstationid']; ?></option>
<?php endforeach; ?>
</select>

Also here at PHP end, you can explode the string into an array and store it further into db.

<?php
$select = explode(',', $select)
print_r($select); // this will have your two values
?>

How to post two values in an option field?

You cannot post two values unless both of them appear in the value attribute. You can place both in, separated by a comma or hyphen and explode() them apart in PHP:

// Place both values into the value attribute, separated by "-"
<option value="<?php echo $name['id'] . "-" . $name['name']);?>">
<?php echo $name['name']);?>
</option>

Receiving them in PHP

// split the contents of $_POST['data'] on a hyphen, returning at most two items
list($data_id, $data_name) = explode("-", $_POST['data'], 2);

echo "id: $data_id, name: $data_name";

How can i send two values from the same select option to a url in php?

Not really sure what you're trying to achieve (why you do redirect instead of direct submit to second file), but still:

 <select name="gender">
<option value="">Select a person:</option>
<option value="Male:boy">Male</option>
<option value="Female:girl">Female</option>
</select>

On server side split the value by ::

<?php
if (isset($_POST['subgen'])) {
$parts = explode(':', $_POST['gender']);?>
<meta http-equiv="refresh" content="0;url=genderreport.php?gender=<?php echo $parts[0]?>&genderid=<?php echo $parts[1]?>"/>
<?php
}

In another page:

if (isset($_GET['gender']) && isset($_GET['genderid'])) {
$gender= $_GET['gender'];
$genderid= $_GET['genderid'];
}

Note, please use spaces in your code, it greatly increases readability.

Can an Option in a Select tag carry multiple values?

One way to do this, first one an array, 2nd an object:

    <select name="">
<option value='{"num_sequence":[0,1,2,3]}'>Option one</option>
<option value='{"foo":"bar","one":"two"}'>Option two</option>
</select>

How can I send two values through a select?

Make the option value have this format: name$id

<select name="category_id" size="1"><br />';

$sql = "SELECT id, name
FROM categories
ORDER BY name";

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs)) {
echo "<option value=\"".$row['name']."$".$row['id']."\">".$row['name']."</option>\n ";
}

echo'
</select>

then when you retrieve the data you can explode it as so

$option = explode("$", $_POST['category_id']);
echo $option[0]; // Name
echo $option[1]; // Id


Related Topics



Leave a reply



Submit