Home > OS >  how can i submit my form in html using ajax into php?
how can i submit my form in html using ajax into php?

Time:11-02

i have a code in html for scanning the qr code `

<form action="attendance.php" method="post"  style="border-radius: 5px;padding:10px;background:#fff;" id="divvideo">
                     <i ></i> <label>SCAN QR CODE</label> <p id="time"></p>
                    <input type="text" name="employee" id="text" placeholder="scan qrcode"    autofocus>
                </form>
</form>

`

and this is my ajax to submit my form in php `

 $('#attendance').submit(function(e){
    e.preventDefault();
    var attendance = $(this).serialize();
    $.ajax({
      type: 'POST',
      url: 'attendance.php',
      data: attendance,
      dataType: 'json',
      success: function(response){
        if(response.error){
          $('.alert').hide();
          $('.alert-danger').show();
          $('.message').html(response.message);
        }
        else{
          $('.alert').hide();
          $('.alert-success').show();
          $('.message').html(response.message);
          $('#employee').val('');
        }
      }
    });
  }

` i want to pass the qr code in my php script and i dont know how to submit it

CodePudding user response:

this is the server side

<?php

if(isset($_POST['employee'])){
    $output = array('error'=>false);

    include 'conn.php';
    include 'timezone.php';

    $employee = $_POST['employee'];
    $status = $_POST['status'];

    $sql = "SELECT * FROM employees WHERE employee_id = '$employee'";
    $query = $conn->query($sql);

    if($query->num_rows > 0){
        $row = $query->fetch_assoc();
        $id = $row['id'];

        $date_now = date('Y-m-d');

        if($status == 'in'){
            $sql = "SELECT * FROM attendance WHERE employee_id = '$id' AND date = '$date_now' AND time_in IS NOT NULL";
            $query = $conn->query($sql);
            if($query->num_rows > 0){
                $output['error'] = true;
                $output['message'] = 'You have timed in for today';
            }
            else{
                //updates
                $sched = $row['schedule_id'];
                $lognow = date('H:i:s');
                $sql = "SELECT * FROM schedules WHERE id = '$sched'";
                $squery = $conn->query($sql);
                $srow = $squery->fetch_assoc();
                $logstatus = ($lognow > $srow['time_in']) ? 0 : 1;
                
                $sql = "INSERT INTO attendance (employee_id, date, time_in, status) VALUES ('$id', '$date_now', NOW(), '$logstatus')";
                if($conn->query($sql)){
                    $output['message'] = 'Time in: '.$row['firstname'].' '.$row['lastname'];
                }
                else{
                    $output['error'] = true;
                    $output['message'] = $conn->error;
                }
            }
        }
        else{
            $sql = "SELECT *, attendance.id AS uid FROM attendance LEFT JOIN employees ON employees.id=attendance.employee_id WHERE attendance.employee_id = '$id' AND date = '$date_now'";
            $query = $conn->query($sql);
            if($query->num_rows < 1){
                $output['error'] = true;
                $output['message'] = 'Cannot Timeout. No time in.';
            }
            else{
                $row = $query->fetch_assoc();
                if($row['time_out'] != '00:00:00'){
                    $output['error'] = true;
                    $output['message'] = 'You have timed out for today';
                }
                else{
                    
                    $sql = "UPDATE attendance SET time_out = NOW() WHERE id = '".$row['uid']."'";
                    if($conn->query($sql)){
                        $output['message'] = 'Time out: '.$row['firstname'].' '.$row['lastname'];

                        $sql = "SELECT * FROM attendance WHERE id = '".$row['uid']."'";
                        $query = $conn->query($sql);
                        $urow = $query->fetch_assoc();

                        $time_in = $urow['time_in'];
                        $time_out = $urow['time_out'];

                        $sql = "SELECT * FROM employees LEFT JOIN schedules ON schedules.id=employees.schedule_id WHERE employees.id = '$id'";
                        $query = $conn->query($sql);
                        $srow = $query->fetch_assoc();

                        if($srow['time_in'] > $urow['time_in']){
                            $time_in = $srow['time_in'];
                        }

                        if($srow['time_out'] < $urow['time_in']){
                            $time_out = $srow['time_out'];
                        }

                        $time_in = new DateTime($time_in);
                        $time_out = new DateTime($time_out);
                        $interval = $time_in->diff($time_out);
                        $hrs = $interval->format('%h');
                        $mins = $interval->format('%i');
                        $mins = $mins/60;
                        $int = $hrs   $mins;
                        if($int > 4){
                            $int = $int - 1;
                        }

                        $sql = "UPDATE attendance SET num_hr = '$int' WHERE id = '".$row['uid']."'";
                        $conn->query($sql);
                    }
                    else{
                        $output['error'] = true;
                        $output['message'] = $conn->error;
                    }
                }
                
            }
        }
    }
    else{
        $output['error'] = true;
        $output['message'] = 'Employee ID not found';
    }
    
}

echo json_encode($output);

?>

CodePudding user response:

You can do like this

<form method="post"  style="border-radius: 5px;padding:10px;background:#fff;" id="divvideo">
    <i ></i> <label>SCAN QR CODE</label> <p id="time"></p>
    <input type="text" name="employee" id="text" placeholder="scan qrcode"    autofocus />
    <input type="submit" id="submit" value="submit" />
</form>

$('#divvideo').submit(function(e){
    e.preventDefault();
    var attendance = $(this).serialize();
    $.ajax({
      type: 'POST',
      url: 'attendance.php',
      data: attendance,
      dataType: 'json',
      success: function(response){
        if(response.error){
          $('.alert').hide();
          $('.alert-danger').show();
          $('.message').html(response.message);
        }
        else{
          $('.alert').hide();
          $('.alert-success').show();
          $('.message').html(response.message);
          $('#employee').val('');
        }
      }
    });
  }

you can get the data in attendance.php file

CodePudding user response:

this is the output i want and i dont know where is the error in my code

  • Related