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!
Aucun commentaire:
Enregistrer un commentaire