注意:未初始化的字符串偏移量:0

时间:2017-07-13 06:52:28

标签: javascript php html ajax

我只想插入不空的行,但是当我尝试多次插入时,我遇到了一些错误。 for循环中存在一个问题,但我无法理解错误,无法解决错误。

错误是:

  

注意:第15行/home2/entigofi/public_html/employee/add_timesheet.php中未初始化的字符串偏移量为0:       注意:未初始化的字符串偏移量:0在第17行的/home2/entigofi/public_html/employee/add_timesheet.php中为0       注意:未初始化的字符串偏移量:在第18行的/home2/entigofi/public_html/employee/add_timesheet.php中为0       注意:未定义索引:第19行/home2/entigofi/public_html/employee/add_timesheet.php中的持续时间
      注意:第20行/home2/entigofi/public_html/employee/add_timesheet.php中未初始化的字符串偏移量为0

php代码在这里:

   <?php
include_once('config.php');


if(isset($_POST['add_timesheet'])){
$cl_time = $_POST['hdnraw'];

            for($i=0; $i<$cl_time; $i++ ){   

                $user_id = $user['id'];
                $client = $_POST['cl_name'][$i];
                $work = $_POST['work_type'][$i];
                $start = $_POST['start'][$i];
                $end = $_POST['end'][$i];
                $duration = $_POST['duration'][$i];
                $discription = $_POST['discription'][$i];
                $status = 1;
                $c_by = $user['id'];
                $c_date = date('Y-m-d H:i:s'); 
                $c_ip = $_SERVER['REMOTE_ADDR'];

                $data5 = array(
                        "user_id" => $user_id,
                        "client_name" => $client,
                        "work_type" => $work,
                        "start_time" => $start,
                        "end_time" => $end,
                        "total_hour" => $duration,
                        "description" => $discription,
                        "status" => $status,
                        "created_by" => $c_by,
                        "created_date" => $c_date,
                        "created_ip" => $c_ip,
                );




                $insert_family_details = $db->insert('om_timesheet_list',$data5);


            }
}

html代码在这里:

<!DOCTYPE html>
<html>

<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<meta charset="utf-8"/>
<title>ENTIGRITYEMPLOYEE | TIMESHEET</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no"/>
<link href="assets/plugins/pace/pace-theme-flash.css" rel="stylesheet" type="text/css"/>
<link href="assets/plugins/bootstrapv3/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="assets/plugins/font-awesome/css/font-awesome.css" rel="stylesheet" type="text/css"/>
<link href="assets/plugins/jquery-scrollbar/jquery.scrollbar.css" rel="stylesheet" type="text/css" media="screen"/>
<link href="assets/plugins/select2/css/select2.min.css" rel="stylesheet" type="text/css" media="screen"/>
<link href="assets/plugins/switchery/css/switchery.min.css" rel="stylesheet" type="text/css" media="screen"/>
<link href="assets/plugins/bootstrap3-wysihtml5/bootstrap3-wysihtml5.min.css" rel="stylesheet" type="text/css"/>
<link href="assets/plugins/bootstrap-tag/bootstrap-tagsinput.css" rel="stylesheet" type="text/css"/>
<link href="assets/plugins/dropzone/css/dropzone.css" rel="stylesheet" type="text/css"/>
<link href="assets/plugins/bootstrap-datepicker/css/datepicker3.css" rel="stylesheet" type="text/css" media="screen">
<link href="assets/plugins/summernote/css/summernote.css" rel="stylesheet" type="text/css" media="screen">
<link href="assets/plugins/bootstrap-daterangepicker/daterangepicker-bs3.css" rel="stylesheet" type="text/css" media="screen">
<link href="assets/plugins/bootstrap-timepicker/bootstrap-timepicker.min.css" rel="stylesheet" type="text/css" media="screen">
<link href="pages/css/pages-icons.css" rel="stylesheet" type="text/css">
<link class="main-stylesheet" href="pages/css/pages.css" rel="stylesheet" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

</head>
<body class="fixed-header " style="background-color:#F5F5F5;">


<form method="post" action="">
<div id="rootwizard" class="m-t-50">

</div>
<div class="container" style="height:auto;">


<div class="row clearfix">

        <div class="col-sm-3" style="width:50px;height:50px;">
                <span style="top:15px;left:2px;" class="glyphicon glyphicon-th-list "></span>
                </div>

        <div class="col-sm-7">
        <h5>TIME SHEET</h5>
                </div>

        <div class="col-sm-3" style="top:8px;width:auto;left:350px;">
        <button type="button" name="add_timesheet" class="btn" style="background-color:red;border-radius:4px;color:white;" /><b>
                <span class="glyphicon glyphicon-plus"></span></b>      
                </button>  

                </div>



</div>
<div class="row clearfix">



<table class="table table-striped" style="background:color:">
<thead>
<tr>

<th>CLIENT NAME</th>
<th>WORK TYPE</th>
<th>START TIME</th>
<th>END TIME</th>
<th>TOTAL HOUR</th>
<th>DESCRIPTION</th>
</tr>
</thead>
<tbody>
<?php for($i=0; $i<5; $i++) {?>
<tr>

<input type="hidden" name="hdnraw" id="hdnraw" value='<?php echo $i;?>'>

<td><input type="text" name="cl_name" class="form-control" /></td>
<td>
<select name="work_type" class="cs-select cs-skin-slide cs-transparent form-control" name="work_type" data-init-plugin="cs-select"/>

<?php $select = $mysqli->query("SELECT * from om_work_type_list where status='1'");
    while($row=$select->fetch_assoc()){
 ?>

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

<?php } ?>
</select>
</td>
<td><input type="text" name="start" class="form-control <?php if($i==0){ ?> firsttimepicker <?php }else{ ?> timepicker<?php } ?>" id="start_<?php echo $i; ?>"/></td>
<td><input type="text" name="end" id="end_<?php echo $i; ?>"  class="form-control timepicker1 ends " /></td>
<td><input type="text" disabled id="diffrence_<?php echo $i; ?>" class="diffrence form-control" name="duration" /></td>
<td><textarea name="discription" class="form-control" cols="20" rows="3" ></textarea></td>
</tr>
<?php  } ?>
</tbody>
</table>
<div class="padding-20">
<button class="btn btn-success  btn-cons from-left  pull-right" type="submit" name="add_timesheet">
<span>SUBMIT</span>
</button>
</div>                 

</div>



</div>
</div>
</div>
</div>
</form>



<script src="assets/plugins/pace/pace.min.js" type="text/javascript"></script>
<script src="assets/plugins/jquery/jquery-1.11.1.min.js" type="text/javascript"></script>
<script src="assets/plugins/modernizr.custom.js" type="text/javascript"></script>
<script src="assets/plugins/jquery-ui/jquery-ui.min.js" type="text/javascript"></script>
<script src="assets/plugins/bootstrapv3/js/bootstrap.min.js" type="text/javascript"></script>
<script src="assets/plugins/jquery/jquery-easy.js" type="text/javascript"></script>
<script src="assets/plugins/jquery-unveil/jquery.unveil.min.js" type="text/javascript"></script>
<script src="assets/plugins/jquery-bez/jquery.bez.min.js"></script>
<script src="assets/plugins/jquery-ios-list/jquery.ioslist.min.js" type="text/javascript"></script>
<script src="assets/plugins/jquery-actual/jquery.actual.min.js"></script>
<script src="assets/plugins/jquery-scrollbar/jquery.scrollbar.min.js"></script>
<script type="text/javascript" src="assets/plugins/select2/js/select2.full.min.js"></script>
<script type="text/javascript" src="assets/plugins/classie/classie.js"></script>
<script src="assets/plugins/switchery/js/switchery.min.js" type="text/javascript"></script>
<script src="assets/plugins/bootstrap-timepicker.js"></script>





<script>

jQuery('body').on('focus',".timepicker", function(){
    jQuery(this).timepicker();
});


jQuery('body').on('focus',".timepicker1", function(){
    jQuery(this).timepicker();
});


jQuery('body').on('focus',".firsttimepicker", function(){
    jQuery(this).timepicker();
});



jQuery('body').on('focus keyup keypress blur change',".ends", function(){
   var id=jQuery(this).attr("id");
   var res = id.split("_");
    var time=jQuery('#start_'+res[1]).val();
    var end=jQuery('#end_'+res[1]).val();

 var hours = parseInt(jQuery('#end_'+res[1]).val().split(':')[0], 10) - parseInt(jQuery('#start_'+res[1]).val().split(':')[0], 10);
         if(hours <= 12) hours = 12 + hours;
         var endt=parseInt(jQuery('#end_'+res[1]).val().split(':')[1], 10);
 if(endt!=00){var minutes = endt - parseInt(jQuery('#start_'+res[1]).val().split(':')[1], 10)}else{
     var minutes = 60-parseInt(jQuery('#start_'+res[1]).val().split(':')[1], 10);
     hours--;
     }
     if(minutes==60){hours++;minutes=minutes-60;} 

        if(minutes<0){hours--;minutes=minutes+60}


jQuery('#diffrence_'+res[1]).val(Math.abs(hours) + ":" +Math.abs(minutes) );
total();

});
</script> 





<script src="pages/js/pages.min.js"></script>

<script src="assets/js/scripts.js" type="text/javascript"></script>

<script src="assets/js/demo.js" type="text/javascript"></script>
<script>
         window.intercomSettings = {
           app_id: "xt5z6ibr"
         };

</body>


</html>

我该怎么做?

2 个答案:

答案 0 :(得分:1)

您需要在html中为每个字段的名称指定数组。同时禁用的值也未提交,因此您可以将其更改为只读。更改此HTML代码:

<?php for($i=0; $i<5; $i++) {?>
<tr>

<input type="hidden" name="hdnraw[]" id="hdnraw" value='<?php echo $i;?>'>

<td><input type="text" name="cl_name[]" class="form-control" /></td>
<td>
<select  class="cs-select cs-skin-slide cs-transparent form-control" name="work_type[]" data-init-plugin="cs-select"/>

<?php $select = $mysqli->query("SELECT * from om_work_type_list where status='1'");
    while($row=$select->fetch_assoc()){
 ?>

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

<?php } ?>
</select>
</td>
<td><input type="text" name="start[]" class="form-control <?php if($i==0){ ?> firsttimepicker <?php }else{ ?> timepicker<?php } ?>" id="start_<?php echo $i; ?>"/></td>
<td><input type="text" name="end[]" id="end_<?php echo $i; ?>"  class="form-control timepicker1 ends " /></td>
<td><input type="text" readonly id="diffrence_<?php echo $i; ?>" class="diffrence form-control" name="duration[]" /></td>
<td><textarea name="discription[]" class="form-control" cols="20" rows="3" ></textarea></td>
</tr>
<?php  } ?>

答案 1 :(得分:0)

错误Undefined index即将到来,因为对象中没有索引。如下所示:

$client = isset($_POST['cl_name'][$i])?$_POST['cl_name'][$i]:'';
$work = isset($_POST['work_type'][$i])?$_POST['work_type'][$i]:'';
$start = isset($_POST['start'][$i])?$_POST['start'][$i]:'';
$end = isset($_POST['end'][$i])?$_POST['end'][$i]:'';
$duration = isset($_POST['duration'][$i])?'';
$discription = isset($_POST['discription'][$i])?'';