确认扫描条形码结果

时间:2015-06-24 07:07:08

标签: javascript php cordova

我有一个带有barcodescanner库的项目用于phonegap。我已成功扫描QR码图像。我想用我的数据库验证QR码内的代码。怎么做? 我有barcodescanner.js:

document.addEventListener("deviceready", init, false);
    function init() {
    }

    function scan() {
        log("scanning");
        cordova.plugins.barcodeScanner.scan(scanSuccessCallback, scanErrorCallback);
    }

    function scanSuccessCallback(result) {
        log(JSON.stringify(result));
        konfirmasi();
        /*
        alert("We got a barcode\n" +
        "Result: " + result.text + "\n" +
        "Format: " + result.format + "\n" +
        "Cancelled: " + result.cancelled);
         */
    }

    function scanErrorCallback(error) {
        alert("Scanning failed: " + JSON.stringify(error));
    }

    function encode() {
        log("encoding");
        if (device.platform == "Android") {  //Not supported on iOS
            var stringToEncode = "http://www.sap.com";
            cordova.plugins.barcodeScanner.encode(cordova.plugins.barcodeScanner.Encode.TEXT_TYPE, stringToEncode, encodeSuccessCallback, encodeErrorCallback);
        }
        else {
            log("Encoding is not supported on iOS.  See https://github.com/wildabeast/BarcodeScanner/issues/106");  
        }
    }

    function encodeSuccessCallback(result) {
        log(JSON.stringify(result));
    }

    function encodeErrorCallback(error) {
        alert("Encoding failed: " + JSON.stringify(error));
    }

    function log(line) {
        var results = document.getElementById("scan_results");
        //results.innerHTML+= "<br>" + line;
    }

这是我的认证php:

<?php 
    header("access-control-allow-origin: *") ;

    include "connect.php";

    $kode_konfirmasi = $_POST['konfirmasi'];
    $kurir = $_POST['kurir'];
    $miss = 'miss';
    $delivered = 'delivered';

    $query_kode = "SELECT * FROM pengiriman WHERE kode_transaksi = '$kode_konfirmasi' AND status = 'Belum Terkirim'";
    $res_kode_ok = mysqli_query($dbc, $query_kode);

    $query_exp = "SELECT * FROM pengiriman WHERE kode_transaksi = '$kode_konfirmasi' AND status = 'Terkirim'";
    $res_kode_expired = mysqli_query($dbc, $query_exp);

    $query_ok = "SELECT pengiriman.kode_transaksi, pengiriman.status FROM pengiriman, kurir WHERE pengiriman.id_kurir = kurir.id_kurir AND kurir.username = '$kurir' AND pengiriman.kode_transaksi = '$kode_konfirmasi'";
    $res_kurir = mysqli_query($dbc, $query_ok);

    if(mysqli_num_rows($res_kode_ok)>0 && mysqli_num_rows($res_kurir)>0){
        $res_upd = mysqli_query($dbc, $query_upd);
        $output = array('status' => true);
        echo json_encode($output);
        $query_upd = "UPDATE pengiriman SET status = 'Terkirim' WHERE kode_transaksi = '$kode_konfirmasi'";
    }elseif(mysqli_num_rows($res_kode_ok)>0 && mysqli_num_rows($res_kurir)==0){
        $output = array('status' => $miss);
        echo json_encode($output);
    }elseif (mysqli_num_rows($res_kode_expired)>0){
        $output = array('status' => $delivered);
        echo json_encode($output);
    }else{
        $output = array('status' => false);
        echo json_encode($output);
    }
?>

我有一个脚本,用于手动验证QR码内的代码,方法是将其插入html格式,如

$(document).on('pageinit','#manual',function(){
    $(document).on('click','#submit',function(){
        if($('#kode').val().length>0){
            var un = window.localStorage.getItem('uname');
            var kode = $('#kode').val();
            $.ajax({
                url:'http://qrkonfirmasi.16mb.com/delivery/konfirmasi.php',
                data:{  kurir : un,
                        konfirmasi : kode
                    },
                type:'post',
                async:'false',
                dataType: 'json',
                beforeSend:function(){
                    $.mobile.loading('show',{theme:"a",text:"Please wait...",textonly:true,textVisible:true});
                },
                complete:function(){
                    $.mobile.loading('hide');
                },
                success:function(hasil){
                    console.log(hasil);
                    if(hasil.status==true){
                        alert('Berhasil! Konfirmasi kode ' + kode + ' berhasil.')
                        $.mobile.changePage("#konfirmasi");
                        console.log('Konfirmasi Berhasil');
                    }else if(hasil.status=='delivered') {
                        alert('Error! Kode '+ kode + ' telah dikonfirmasi.');
                    }else if(hasil.status=='miss'){
                        alert('Error! Data kurir tidak sesuai.');
                    }else{
                        alert('Gagal, Kode konfirmasi tidak sesuai.');
                    }
                },
                error:function(request,error){
                    alert('Koneksi error. Silahkan coba beberapa saat lagi!');
                }
            });
        }else{
            alert('Masukkan username dan password!');
        }
        return false;
    });
});

那么,当扫描成功时,我如何立即从扫描结果中验证代码?

0 个答案:

没有答案