如何在基于条件的PHP中启用禁用选项值

时间:2019-06-11 01:30:09

标签: javascript php ajax select drop-down-menu

用户选择位置后,在下一个选择选项中,即选择汽车,将显示所选位置的汽车列表。直接从数据库中检索汽车列表。所以我需要显示该区域中的所有汽车列表并需要根据以下条件禁用启用汽车列表:如果汽车ID不可用,则选项值将被禁用。如果汽车ID可用,则选项值将变为启用。

        <?php

        $car = "SELECT *,location_master.location_id , location_master.location_name, 
        appcarinfo.loc_id_ext, appcarinfo.location ,appcarinfo.model ,appcarinfo.noplat FROM location_master 
       INNER JOIN appcarinfo ON
        appcarinfo.lat = location_master.gmaplat
        AND
        appcarinfo.lon = location_master.gmaplng
        where appcarinfo.model='".$fetchres['idmodel']."'  ";

        $qcar = mysqli_query($conn, $car);
        ?>
       <option disabled value="" selected hidden>Please Select Car</option>
        <?php
        while ($showcar= mysqli_fetch_array($qcar))
        {

if ($showcar['car_id']=="Available")
{     
?>
        <option  class="<?php echo $showcar['lon']; ?>" value="<?php echo $showcar['car_id']; ?>" enabled> <?php echo $fetchres['maker'].' '.$fetchres['model_name'].'-'.$showcar['noplat'].' ' .$showcar['location_name']; ?></option>

         <?php
  }
 else
 ?> <option  class="<?php echo $showcar['lon']; ?>" value="<?php echo        $showcar['car_id']; ?>" disabled> <?php echo $fetchres['maker'].'      '.$fetchres['model_name'].'-'.$showcar['noplat'].' '     .$showcar['location_name']; ?></option>
  <?php
             }
          ?>
        </select>

SELECT OPTION

我也是AJAX的新手。如何传递数据并将其取回以选择选项

 //check availability car
function check_availability() 
{
    //id from form
    var reservation1 = document.getElementById("reservation");
    var pickup_date = document.getElementById("pickup_date").value;
    var return_date = document.getElementById("return_date").value;
    var pickup_time = document.getElementById("pickup_time").value;
    var return_time = document.getElementById("return_time").value;
    var carID= document.getElementById("carID").value;
            $.ajax({
                type : "POST",
                url : "function/check_car_availability.php",
                data : {
                    pickup_date : pickup_date,
                    return_date : return_date,
                    pickup_time : pickup_time,
                    return_time : return_time,
                    carID : carID,

                },
                dataType : "JSON",
                success : function(data) {
                    $('#edt_pickup').val(data.edt_pickup);
                    $('#edt_return').val(data.edt_return);
                    $('#msgCheck').html(data.msgCheck);
                    $('#btn_proceed').html(data.btn_proceed);

                }
            }); 
}

check_car_availability.php

      <?php
if ($_POST['pickup_date']) 
{
$pickup_date = $_POST['pickup_date'];
$return_date = $_POST['return_date'];
$pickup_time = $_POST['pickup_time'];
$return_time = $_POST['return_time'];
$car_id = $_POST['car_id'];
$owner_id = $_POST['owner_id'];

//convert normal date to epoch
$pickup_date1 = explode("/",$pickup_date);
$return_date1 = explode("/",$return_date);
$pickup_time1 = explode(":",$pickup_time);
$return_time1 = explode(":",$return_time);

//hour, minute, second, month, day, year
$edt_pickup = mktime($pickup_time1[0],$pickup_time1[1],0,$pickup_date1[1],$pickup_date1[0],$pickup_date1[2]);
$edt_return = mktime($return_time1[0],$return_time1[1],0,$return_date1[1],$return_date1[0],$return_date1[2]);

//convert from d/m/Y to Y-m-d
$pickup_date_2 = $pickup_date1[2]."-".$pickup_date1[1]."-".$pickup_date1[0];
$return_date_2 = $return_date1[2]."-".$return_date1[1]."-".$return_date1[0];

//keluarkan tarikh yg customer pilih ada tak dlm booking master
$chkBooked = mysqli_query($conn, "SELECT count(car_id) AS countid FROM appbooking WHERE 
((start_rent >= '$edt_pickup' AND end_rent <= '$edt_return') 
OR (((start_rent <= '$edt_pickup' AND end_rent >= '$edt_return')))
OR (((end_rent >= '$edt_pickup' AND end_rent <= '$edt_return')))
OR (((start_rent >= '$edt_pickup' AND start_rent <= '$edt_return'))))  AND car_id = '$car_id'  AND status !='0'");
$fetchBooked = mysqli_fetch_array($chkBooked);

if($fetchBooked['countid'] != 0) //kalau ada show not available
{   
    $availability = "Not Available";
    $available_count = 0;
} else //kalau tak de show available
{
    $availability = "Available";
    $available_count = 1;
} 

//keluarkan car details
$carDetails = mysqli_query($conn, "SELECT * FROM appcarinfo WHERE car_id = '$car_id'");
$fetchchkowner = mysqli_fetch_array($carDetails);



//kalau manual availability check ada tak date dlm range yg owner dah set
$sql = mysqli_query($conn, "SELECT count(car_availability.availability_id) AS cntAvail FROM appcarinfo INNER JOIN car_availability ON car_availability.car_id = appcarinfo.car_id WHERE appcarinfo.car_id = '$car_id' AND car_availability.start_available <= '$edt_pickup' AND car_availability.end_available >= '$edt_return'");
$fetch = mysqli_fetch_array($sql);

if ($fetchchkowner['custom_availability'] == 1) //if owner set manual availability
{
    if ($fetch['cntAvail'] > 0) //kalau ada 
    {
        $avail = 1;
    } else //kalau tak de
    {
        $avail = 0;
    }
} else //if owner set auto availability
{
    $avail = 1;
}

//check if this is klezcar car
if($fetchchkowner["owner_id"] == "0")
{
    $klezcar_loc_id = $fetchchkowner["loc_id_ext"]; 

    //check availability for blocked date klezcar
    $sqlBlock = "SELECT * FROM `blocked_date_klezcar` where (
                ('".$pickup_date_2."' > startdate and '".$pickup_date_2."' < enddate)
                or ('".$return_date_2."' > startdate and '".$return_date_2."' < enddate)
                or ('".$pickup_date_2."' = startdate)
                or ('".$return_date_2."' = enddate)
                or ('".$return_date_2."' = startdate)
                or ('".$return_date_2."' = enddate)
                ) and (location=0 or location=".$klezcar_loc_id.") order by location desc limit 1";
    $queryBlock = mysqli_query($conn,$sqlBlock);
    if(mysqli_num_rows($queryBlock) > 0)
    {
        $resBlock = mysqli_fetch_object($queryBlock);
        $avail = 0;
        $klezcar_blocked = 1;
        $klezcar_blocked_reason = $resBlock->reason;
    }
    else
    {
        $klezcar_blocked = 0;
    }
 }

//calculation price rate
include 'calculation_price/calculationprice.php';

$encry = md5($edt_pickup.$edt_return.$total_pay.$total_rate.floor($day).$bhourplus.$car_id.$owner_id.$secretAuth);

//combine appbooking & car availability variable
if ($avail == 1 && $available_count == 1) 
{
    $availStatus = "<div class='alert alert-success'>You are good, car available.<br>Rental Price : Total <span style='font-size:20px; font-weight: bold;'>".number_format($total_pay,2)."</span> for ".floor($day)." day(s) ".$bhourplus." hour(s)</div>";
    $btn_proceed = "<a href='confirmbooking.php?pickupdate=".$edt_pickup."&returndate=".$edt_return."&totalpay=".$total_pay."&totalrate=".$total_rate."&bookday=".floor($day)."&bookhour=".$bhourplus."&carid=".$car_id."&ownerid=".$owner_id."&encry=".$encry."' class='btn btn-primary'>Proceed</a>";
} else
{
    $availStatus = "<div class='alert alert-danger'>We are sorry, car not available. Please refer Available & Not available date table.</div>";
    $btn_proceed = "";
    if($klezcar_blocked === 1)
    {
        $availStatus = "<div class='alert alert-danger'>".$klezcar_blocked_reason."</div>";
        $btn_proceed = "";
    }
}
    /* *** SPECIAL FOR RAYA 2019 *** */
    $start_blocked_date = "2019-06-04";
    $end_blocked_date   = "2019-06-09";


    if(  ($pickup_date_2 >= $start_blocked_date  && $pickup_date_2 <= $end_blocked_date) 
      || ($return_date_2 >= $start_blocked_date  && $return_date_2 <= $end_blocked_date)
      || ($pickup_date_2 == $start_blocked_date)
      || ($return_date_2 == $end_blocked_date)
      || ($return_date_2 == $start_blocked_date)
      || ($return_date_2 == $end_blocked_date))
    {
        $availStatus = "<div class='alert alert-danger'>The date has been marked as Hari Raya Aidlifitri holiday session. We accept minimum rental of 7 days and above only. Please re-select date at least 3/6/2019 - 10/6/2019.</div>";
         $btn_proceed = "";
    }
    /* *** SPECIAL FOR RAYA 2019 *** */

//return value
$data['edt_pickup'] = $edt_pickup;
$data['edt_return'] = $edt_return;
$data['msgCheck'] = $availStatus;
$data['btn_proceed'] = $btn_proceed;
$data['total_rate'] = $total_rate;
$data['total_pay'] = $total_pay;
$data['no_day_booking'] = floor($day);
$data['no_hour_booking'] = $bhourplus;

echo json_encode($data);
}
?>

0 个答案:

没有答案