通过ajax调用启用禁用字段

时间:2016-10-18 11:52:46

标签: php jquery ajax

我正在为员工进行专业管理。在下面的附件中有3个输入字段(一个下拉列表,2个日历字段)。在下拉列表中,有员工姓名。在第二个字段中,它们是当前日期和当前签入时间,在第三个字段中有员工的结账时间。我想要,

1)当我从下拉列表中选择名称时,根据名称2显示日历字段。例如,下拉列表中有名称(Ramandeep kaur)。我选择ramandeep kaur。 onselect下拉值我检查了table name ='ramandeep'和date ='当前日期',然后我的cinder字段for checkin将禁用,我的结帐div将启用。如果我的签入字段在数据库中为空,则checkout字段将禁用。我的代码工作正常,但我不知道如何启用或禁用日历字段。

enter image description here

这是我的HTML

<form id="form_validation" class="uk-form-stacked" method="post" action="code.php">
    <div class="uk-grid" data-uk-grid-margin>

        <div class="uk-width-medium-1-4">

            <div class="parsley-row">

                  <select name="name" id="val_select" class="form control" onChange="getname(this.value);" required>
                    <option value="">Choose..</option>
<?php 
    $employee = mysqli_query($cn, "SELECT * from `pg_employee`");

    while($attendence = mysqli_fetch_array($employee)){
?>

                    <option value="<?php echo $attendence['id']; ?>"><?php echo $attendence['name']; ?></option>
<?php } ?>

                </select>
            </div>
        </div>

            <div class="uk-width-medium-1-4">

                <div class="parsley-row">

                    <div id="datetimepicker" class="input-append date" style="border:#fff;">
                          <input type="md-input" class="form-control" name='check_in' style="padding:4px; border:1px solid #ccc;" value="<?php 
                          $date = date('d-M-Y'); 
                          echo $date;
                          ?>
                          "/></input>
                          <span class="add-on" style="background:#fff;">
                            <i data-time-icon="icon-time" style="font-size:60px;" data-date-icon="icon-calendar"></i>
                          </span>
                    </div>
                </div>
            </div>
            <div class="uk-width-medium-1-4">

                <div class="parsley-row">

                    <div id="datetimepicker2" class="input-append date" style="border:#fff;">
                          <input type="md-input" class="form-control" name="check_out" style="margin-left:20px; padding:4px; border:1px solid #ccc;"></input>
                          <span class="add-on" style="background:#fff;">

                            <i data-time-icon="icon-time" style="font-size:60px;" data-date-icon="icon-calendar"></i>
                          </span>
                    </div>
                </div>
            </div>

             <div class="uk-width-medium-1-4">

                <div class="parsley-row">

                     <button type="submit" name="attendence_date" style="margin-left:40px;" class="md-btn md-btn-primary">Submit</button>

                 <div id="name-list">
                 </div>
            </div>
        </div>

        </div>
        <div class="uk-grid">


        <div class="uk-width-medium-1-5">
            <div class="parsley-row">


            </div>
        </div>

        <div class="uk-width-medium-1-5">

            <div class="parsley-row">

            </div>

        </div>

    </div>

     <?php if($_GET['msg'] && $_GET['msg']=='create'){

            echo"<div style='color:red; text-align:center; font-weight:bold; font-size:16px;'>New record added</div>";

        }?>
      </form>

这是我的ajax调用代码

<script>
function getname(val) {
     $.ajax({
        type: "POST",
        url: "manage_name.php",
        data:'attendence_id='+val,
        success: function(data){
            $("#name-list").html(data);
        }
    });
}
</script>

这是我的manage_name.php文件代码

 <?php
  date = date("Y-m-d");

 if(!empty($_POST["attendence_id"])) {

 $id = $_POST["attendence_id"];

 $employee = mysqli_query($cn, "SELECT * from `pg_attendence` WHERE emp_name ='$id' AND date ='$date'");
 $zxc = mysqli_num_rows($employee);
 if($zxc > 0){?>

<?php   }
}

?>

1 个答案:

答案 0 :(得分:0)

<?php
  date = date("Y-m-d");

 if(!empty($_POST["attendence_id"])) {

 $id = $_POST["attendence_id"];

 $employee = mysqli_query($cn, "SELECT * from `pg_attendence` WHERE emp_name ='$id' AND date ='$date'");
 $zxc = mysqli_num_rows($employee);
 if($zxc > 0){?>
  echo 1;
<?php   }
else {
  echo 0;
}
}

?>

在你的ajax回调中替换代码

success: function(data){
            if(data == 1) {
                 $(".datetimepicker2 input").attr("disabled", disable);
            }
            else {
                 $(".datetimepicker input").attr("disabled", disable);
            }
}