  | 
              
	              
	                
  
    
	 
        [PHP] สอบถามปัญหาการส่งข้อมูลจาก Form โดยใช้ ajax ว่าจะต้องทำอย่างไร     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                พอดีผมจะทำระบบลงเวลาพนักงาน โดยมีช่องใส่รหัส rfid เวลาสแกนบัตรเครื่องอ่านาจะพิมพ์ให้เอง ทีนี้ผมจะลองใช้ ajax ส่งข้อมูล rfid ไปยังอีกไฟล์นึงที่มีคำสั่งในการ insert / update ข้อมูล แต่ติดปัญหาตรงที่มันไม่ส่งข้อมูลไปยังอีกที่นึงและไปดู db ก็ยังไม่บันทึก ไม่ทราบว่าติดตรงไหน รบกวนด้วยนะครับ 
 
concept : 
user แตะบัตร เครื่องอ่านจะใส่ rfid ลงใน textbox แล้วตัวระบบจะแจ้งว่าเข้างาน / ออกงานแล้ว โดยที่ไม่ต้องกดปุ่มเพื่อ submit form 
 
  
 
index.php 
Code (PHP) 
<?php include("sql/connect.php") ?>
<?php include("header.php") ?>
<body>
<style>
.jumbotron{
    background-color: #C70039;
    color: #ffffff;
}
</style>
<!-- time -->
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(function(){
 
 
    var nowDateTime=new Date("<?=date("m/d/Y H:i:s")?>");
    var d=nowDateTime.getTime();
    var mkHour,mkMinute,mkSecond;
     setInterval(function(){
        d=parseInt(d)+1000;
        var nowDateTime=new Date(d);
        mkHour=new String(nowDateTime.getHours());  
        if(mkHour.length==1){  
            mkHour="0"+mkHour;  
        }
        mkMinute=new String(nowDateTime.getMinutes());  
        if(mkMinute.length==1){  
            mkMinute="0"+mkMinute;  
        }        
        mkSecond=new String(nowDateTime.getSeconds());  
        if(mkSecond.length==1){  
            mkSecond="0"+mkSecond;  
        }   
        var runDateTime=mkHour+":"+mkMinute+":"+mkSecond;        
        $("#css_time_run").html(runDateTime);    
     },1000);
 
 
});
 $(function () {
 $("#btnstamp").click(function () {
     var data = $("#frm_stamp").serialize();
 $.ajax({
 url: "stamp.php",
 type: "post",
 data: {txt_rfid: $("#txt_rfid").val()},
 success: function (data) {
 $("#result").html(data);
 }
 });
 });
 $("#frm_stamp").on("keyup keypress",function(e){
 var code = e.keycode || e.which;
 if(code==13){
 $("#btnstamp").click();
 return false;
 }
 });
 });
</script>
<div class="jumbotron jumbotron-fluid text-center">
<img class="mb-4" src="http://dekcom-chamnong.com/news/wp-content/uploads/2016/07/cropped-logo-300x300.png" width="150" height="150">
  <h1 class="display-4">ระบบลงเวลาทำงานครู โรงเรียนจำนงค์วิทยา</h1>
  <h2 class="display-4"><div id="css_time_run"><?=date("H:i:s")?></div></h2>
</div>
<div class="container">
<form name="frm_stamp" id="frm_stamp">
  <div class="form-group row">
  <input type="text" name="txt_rfid" id="txt_rfid" class="form-control text-center" autofocus>
</div>
<div class="form-group row">
<button class="btn btn-primary" type="button" id="btnstamp">stamp</button>
</div>
</form>
</div> 
<br>
<div class="container" id="result">
</div>
<?php include("footer.php") ?>
 
 
stamp.php (ไฟล์นี้ทำหน้าที่บันทึกข้อมูลการเข้างาน) 
Code (PHP) 
<?php include("sql/connect.php") ?>
<?php include("header.php") ?>
<body>
<?
function timethai($strDate)
	{
		
		$strHour= date("H",strtotime($strDate));
		$strMinute= date("i",strtotime($strDate));
		$strSeconds= date("s",strtotime($strDate));
    return "$strHour:$strMinute:$strSeconds น.";
  }
//time
date_default_timezone_set('Asia/Bangkok');
$current_time = date("H:i:s");
$current_date = date("Y-m-d");
$txt_rfid = $_POST['txt_rfid'];
//check user
$sql_name = "SELECT * FROM employee WHERE rfid = '$txt_rfid'";
$qry_name = mysqli_query($con,$sql_name);
$show_name = mysqli_fetch_array($qry_name);
$emp_id = $show_name['emp_id'];
//check status
$sql_status = "SELECT chk_status FROM timesheet WHERE emp_id = '$emp_id";
$qry_status = mysqli_query($con,$sql_status);
$show_status = mysqli_fetch_array($qry_status);
if ($show_status['chk_status'] = ""){
    //stamp time
    $sql_stamp = "INSERT INTO timesheet(emp_id,stamp_date,time_start,time_end,chk_status)
    VALUES('$emp_id','$current_date','$current_time','$current_time','1') ";
    $add_stamp = mysqli_query($con,$sql_stamp);
    if($add_stamp){
    echo '<h1 class="text-center text-primary">';
    echo $show_name['firstname'] . ' ' . $show_name['lastname'] . ' เข้างานแล้ว เวลา ' . timethai($current_time);
    echo '</h1>';
    }
    else{
        echo '<h1 class="text-center text-primary">';
        echo 'ERROR';
        echo '</h1>';
    }
    mysqli_close($con);
}
else{
    //stamp time
    $sql_stamp = "UPDATE timesheet set time_end = '$current_time' WHERE emp_id = '$emp_id'";
    $up_stamp = mysqli_query($con,$sql_stamp);
    if($up_stamp){
    echo '<h1 class="text-center text-primary">';
    echo $show_name['firstname'] . ' ' . $show_name['lastname'] . ' ออกงานแล้ว เวลา ' . timethai($current_time);
    echo '</h1>';
    }
    else{
        echo '<h1 class="text-center text-primary">';
        echo 'ERROR';
        echo '</h1>';
    }
    mysqli_close($con);
}
?>
 
 
  Tag : PHP, HTML5, JavaScript, Ajax, jQuery               
                        | 
           
          
            
		
  ประวัติการแก้ไข 2018-05-21 21:11:34	
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2018-05-21 21:10:27 | 
                      By :
                          ichang97 | 
                      View :
                          1203 | 
                      Reply :
                          2 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code สั้นนิดเดียวเองครับ ลองดีบัคดูทีละ step ว่ามันไปตายตอนไหน ไม่น่ายากน่ะ ใช้ google chrome กด F12 ดูตรง Network ว่ามันส่งอะไรไปและตอบอะไรมา                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-05-22 08:54:20 | 
                        By :
                            mongkon.k | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 $("#frm_stamp").on("keyup keypress",function(e){ ==> on('change',                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-05-22 13:25:14 | 
                        By :
                            x | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |