dongyou2279 2018-12-19 15:36
浏览 195

如果选中复选框,则显示/隐藏下拉菜单(多个) - 相同的类名称php

I have an issue with showing/hiding dropdown menus if a checkbox is checked.

If a checkbox is checked, I want to be able to choose the quantity from a dropdown menu (like an e-commerce site). Since I fetch the results the checkboxes and dropdowns have the same name and classname - this is the part where I get confused since I want to use the same jQuery/javascript to show/hide the dropdown menu. At the moment, all dropdowns are not hidden which leads to an error when using $_POST because all dropdowns will be sent as an array but is not matched with the amount of indexes as the checkboxes - therefore I want to disable or display:none on the dropdowns that are not checked ($_POST only works if I click on all checkboxes since it will match with the amount of dropdowns)

Here is the code:

<?php
    include('db_connect.php');

    $sql = "SELECT * FROM Entertainment";
    $result = mysqli_query($conn, $sql);

    if(!$result)
        die(mysqli_error());

    //Fetch array from Entertainment table in DB
    while ($row = mysqli_fetch_array($result)) {
        $seats = $row['seats']; 
        //needed to disable user from choosing more tickets than it exists
        if ($seats <= 0) {
            echo '<input disabled type="checkbox" name="nojen[]" value="' . $row['entId'] . '">' . $row['entName'] . ' - '  . $row['date'] . ' - '  . $row['time'] . ' - '  . $row['seats'] . ' platser kvar <br>';
        }
        else {
            echo '<input type="checkbox" class="nojen" name="nojen[]" value="' . $row['entId'] . '">' . $row['entName'] . ' - '  . $row['date'] . ' - '  . $row['time'] . ' - '  . $row['seats'] . ' platser kvar <br>';
        }
        echo '<select class="antal" name="antal[]">';
            //if there's not enough seats, disable
            for ($x = 1; $x <= 6; $x++) {
                if ($x > $seats) {
                    print "<option disabled value = \"$x\">$x</option>";
                }
                else {
                    print "<option value  = \"$x\">$x</option>";
                }
            }
        echo '</select>';
    }
    ?>

$_POST:

<?php
    include 'db_connect.php';
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    print_r($_POST);

    $firstname = $_POST['fnamn'];
    $lastname = $_POST['enamn'];
    $email = $_POST['email'];
    $phonenumber = $_POST['tel'];
    $tickets = $_POST['antal'];
    $entid = $_POST['nojen'];

    mysqli_query($conn, "INSERT INTO Customer (firstName, lastName, email, phoneNumber) VALUES('$firstname', '$lastname', '$email', '$phonenumber')");

    $customerid = mysqli_insert_id($conn);

    if(!empty($entid)) {
        foreach(array_combine($entid, $tickets) as $entarray => $ticketsarray) {
            mysqli_query($conn, "INSERT INTO Reservation (entId, customerId, tickets) VALUES('$entarray', '$customerid', '$ticketsarray')");
            mysqli_query($conn, "UPDATE Entertainment SET seats = seats - '$ticketsarray' WHERE entId='$entarray'");
        }
    }
    else {
        echo "Error"  . $conn->error;;
    }
echo "Success";
}
    else {
        echo "Error"  . $conn->error;;
    }
mysqli_close($conn);
?>

jQuery:

$(document).ready(function(){
$('[name="nojen[]"]').change(function(){
    if(this.checked)
        $('[name="antal[]"]').fadeIn('slow');
    else
        $('[name="antal[]"]').fadeOut('slow');

});

});

I know that the checked checkbox must know which dropdown it should show or not, but I am unable to figure out why at the moment... Thanks in advance!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题