简体   繁体   中英

Is it possible to pass two (2) values from 1 radio button when clicked?

I have here a numerous amount of radio buttons and their values are taken from a database. I want to add the type of leave but I can't figure it out. To make myself more clear, once the "Sick" radio button is clicked and when pressing the submit button, the "Sick" credits or the available hours for Sick leave (The value of my radio button which is the (value=$sLeave) and at the same time, the type of leave which is the Sick leave (Hidden type) will be passed to the next page.

I tried this one in every type of leave:

 if($sLeave!=0)
 {
 ?> 

<input type="radio" name="optLeave" value="<?php echo $sLeave; ?>" onclick="disableTextarea()" /><label for="Sick">Sick</label>
    <input type="hidden" name"leaveType" value="Sick" />

<?php
} 
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label  for="Sick">Sick</label>

<?php
}
?>

BUT all of the type of leave will be passed to the next page and it outputs the last hidden value. Not the one that is selected.

Here's the code:

<form id="leaveForm" name="lform" method="get" action="leaveFiled.php" />

if($sLeave!=0)
{
?>  
<input type="radio" name="optLeave" value="<?php echo $sLeave; ?>" onclick="disableTextarea()" /><label for="Sick">Sick</label>

<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Sick">Sick</label>


<?php
}
?>

<?php
if($vLeave!=0)
{
?>
<input type="radio" name="optLeave" value="<?php echo $vLeave; ?>" onclick="disableTextarea()" /><label for="Vacation">Vacation</label>

<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Vacation">Vacation</label>

<?php
}
?>

<?php                   
if($eLeave!=0)
{
?>
<input type="radio" name="optLeave" value="<?php echo $eLeave; ?>" onclick="disableTextarea()" /><label for="Emergency">Emergency</label>

<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Emergency">Emergency</label>

<?php
}
?>

<?php                   
if($mLeave!=0 && $sex=='F')
{
?>
<input type="radio" name="optLeave" value="<?php echo $mLeave; ?>" onclick="disableTextarea()" /><label for="Maternity">Maternity</label>

<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Maternity">Maternity</label>

<?php
}
?>

<?php                   
if($pLeave!=0 && $sex=='M')
{
?>
<input type="radio" name="optLeave" value="<?php echo $pLeave; ?>" onclick="disableTextarea()" /><label for="Paternity">Paternity</label>

<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Paternity">Paternity</label>

<?php
}
?>

<?php                   
if($uLeave!=0)
{
?>
<input type="radio" name="optLeave" value="<?php echo $uLeave; ?>" onclick="disableTextarea()" /><label for="Union">Union</label>

<?php
}
else
{
?>
<input type="radio" name="optLeave" disabled="disabled" /><label for="Union">Union</label>

<?php
}
?>

<?php                   
if($oLeave!=0)
{
?>

<tr>
<td height="40" colspan="3" align="left">
    <strong>
    <input type="radio" name="optLeave" value="<?php echo $oLeave; ?>" onclick="enableTextarea()" />    
    <label for="Other Leave">Other Leave</label>
    &nbsp;&nbsp;
    <label for="Reason of Leave">Reason of Leave:</label>
    <input type="text" name="reasonLeave" size="35" disabled="disabled" />        
    </strong> 
</td>
</tr> 

<?php
}
else
{
?>
<tr>
<td height="40" colspan="3" align="left">
    <strong>
    <input type="radio" name="optLeave" disabled="disabled" />
    <label for="Other Leave">Other Leave</label>
    </strong>
</td>
</tr> 

<?php
}                       
?>

Maybe the use of javascript may solve this problem but I'm hoping for another solution to this.

If you can use diffrent prefix with unique separator like underscore( ) or colon(:) whatever u want in values eg. sleave for sick leave or else

<input type="radio" name="optLeave" value="<?php echo "sleave_".$sLeave; ?>" onclick="disableTextarea()" /><label for="Sick">Sick</label>

<input type="radio" name="optLeave" value="<?php echo "uleave_".$uLeave; ?>" onclick="disableTextarea()" /><label for="Union">Union</label>

then in post u can separate the value by explode function and you will get the your values.

//use same separator here
$opt_leave = explode("_", $_REQUEST['optLeave']);
$leave_type = $opt_leave[0];
$leave = $opt_leave[1];
//now you can use $leave_type to check your conditions by switch or if conditional statements

if($leave_type == 'sleave') {
  // action on sick leave
} else if($leave_type == 'uleave') {
 // action on Union leave
}

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