如何创建php函数或任何脚本/jquery Javascript,使我能够获取与复选框关联的值并添加它们



//在选择经济舱、经济舱或头等舱之前,用户只能选择座位号复选框,如果用户单击其中一个经济舱/头等舱复选框而没有选择座位号框,则会出现一条弹出消息或验证消息,指示必须先选择座位号。

此外,价格值由检查的座位数量决定。例如,假设票的价格是120美元,他选择了座位号2的复选框和座位号1的复选框,那么总数应该是240美元,依此类推。好座位、经济舱和头等舱复选框不会增加价格。他们在那里只是为了安排座位。为了让用户单击add_submit按钮,必须至少选中一个复选框。我该如何创建?有人请帮帮我。我必须在我的会话脚本中键入什么,这样一旦做出选择并单击"添加_提交"按钮,下一页就会记住总数和旅行详细信息。在此处输入图像描述

Here is my php page
<!DOCTYPE html>
<head>
<style>
td{
    padding-top: 10px; 
    padding-left: 10px;
    padding-right: 10px;
    padding-bottom: 10px;
}
p{
font-size: 16px;
}
</style>
<body>
<?php
// Start the session
session_start();
?>
<?php
       $str = $_GET['Confirm'];
       $array = (explode(",",$str)); 
      ?>

<h1>Booking Details</h1>
travel Details:
<table>
    <tr>
        <td> key id
        </td>
        <td><?php echo $array[0] ?>
        </td>
    </tr>
    <tr>
        <td>
        destination</td>
        <td> <?php echo $array[1] ?>
        </td>
    </tr>
    <tr>
        <td>
        arrival</td>
        <td> <?php echo $array[2] ?>
        </td>
    </tr>
    <tr>
        <td>
        Price</td>
        <td> $<?php echo $array[3] ?>
        </td>
    </tr>
</table>
<?php
// Set session variables
$_SESSION["key_id"] = $array[0];
$_SESSION["destination"] = $array[1];
$_SESSION["arrival"] = $array[2];
$_SESSION["price"] = $array[3];
echo "Session variables for this booking have been set.";
?>
<form action="Yourbookings.php" method="get">
<table>
<tr>
    <td>Seat #</td>
    <td>econ </td>
    <td>firstclass</td>
    <td>goodseat</td>
</tr>
<tr>
    <td>Seat 1 <input type="checkbox" name="seat1" value="seat1"> </td>
    <td>  <input type="checkbox" name="econ" value="econ"> </td>
    <td>  <input type="checkbox" name="firstclass" value="firstclass"> </td>
    <td>  <input type="checkbox" name="goodseat" value="goodseat"> </td>
</tr>
<tr>
    <td>Seat 2 <input type="checkbox" name="seat1" value="seat1"> </td>
    <td>  <input type="checkbox" name="econ" value="econ"> </td>
    <td>  <input type="checkbox" name="firstclass" value="firstclass"> </td>
    <td>  <input type="checkbox" name="goodseat" value="goodseat"> </td>
</tr>
<tr>
    <td>Seat 3 <input type="checkbox" name="seat1" value="seat1"> </td>
    <td>  <input type="checkbox" name="econ" value="econ"> </td>
    <td>  <input type="checkbox" name="firstclass" value="firstclass"> </td>
    <td>  <input type="checkbox" name="goodseat" value="goodseat"> </td>
</tr>
<tr>
    <td>Seat 4 <input type="checkbox" name="seat1" value="seat1"> </td>
    <td>  <input type="checkbox" name="econ" value="econ"> </td>
    <td>  <input type="checkbox" name="firstclass" value="firstclass"> </td>
    <td>  <input type="checkbox" name="goodseat" value="goodseat"> </td>
</tr>
<tr>
    <td>Seat 5 <input type="checkbox" name="seat1" value="seat1"> </td>
    <td>  <input type="checkbox" name="econ" value="econ"> </td>
    <td>  <input type="checkbox" name="firstclass" value="firstclass"> </td>
    <td>  <input type="checkbox" name="goodseat" value="goodseat"> </td>
</tr>

</table>
<p>Total = $</p>
// what code do I create to so that it adds the ticket prices based on how many seats are selected.
<?php
$_SESSION["price"] = $array[3];

?>

<input type="submit" name="Add booking" value="Add_booking">
</form>

<table>
<td>
<form action="add_booking.php" method="get">
</td>
</table>
</body>
</head>
</html>

我想我理解你在说什么,我想你会想用javascript来做这件事,还有单选按钮(我想)、隐藏价格字段和readonly字段:

演示:

https://jsfiddle.net/h6aawjf0/

脚本:

$(document).ready(function(){
    $(this).on('click','.clearit',function(e) {
        e.preventDefault();
        $('input[type=radio]').attr('checked',false);
        $('input[name=total]').val('0');
    });
    $(this).on('change','input[type=radio]',function() {
        var radioBtns   =   $('input[type=radio]');
        var price       =   $('input[name=price]').val();
        var sum         =   0;
        $.each(radioBtns,function(k,v) {
            if($(v).is(":checked"))
                sum +=  1;
        });
        $('input[name=total]').val((sum*price));
    });
});
</script>
<form action="Yourbookings.php" method="get">
    <input type="hidden" name="price" value="120" />
    <table>
        <tr>
            <td>Seat #</td>
            <td>econ </td>
            <td>firstclass</td>
            <td>goodseat</td>
        </tr>
        <?php for($i=1; $i<5;$i++) { ?>
        <tr>
            <td>Seat <?php echo $i; ?>
            <td><input type="radio" name="<?php echo $i; ?>" value="econ"></td>
            <td><input type="radio" name="<?php echo $i; ?>" value="firstclass"></td>
            <td><input type="radio" name="<?php echo $i; ?>" value="goodseat"></td>
        </tr>
        <?php } ?>
    </table>
        <input type="text" name="total" readonly value="0" />
    <input type="submit" name="Add booking" value="Add_booking">
    <input type="submit" class="clearit" value="CLEAR">
</form>

选择并提交后的输出将类似于:

Array (
        [price] => 120
        [1] => econ
        [2] => firstclass
        [3] => firstclass
        [4] => econ
        [total] => 480
        [Add_booking] => Add_booking
    )

相关内容

  • 没有找到相关文章

最新更新