如何在服务器接收到作为QR扫描仪的客户端手机的QR扫描结果后,在php页面上显示查询结果


How to display the query result on the php page after the server receive QR scan result from client phone which act as QR Scanner

我使用PhoneGap创建一个Android QR码扫描应用程序,作为客户端,而服务器使用PHP与MySQL (WAMP)。下面是QR扫描应用程序发送扫描结果到服务器的部分,它设法发送并获得服务器的回复,所以我认为问题在下一节

中的服务器代码中。
$.ajax({
    url: "http://192.168.1.2/receiveQR.php",
    type: 'POST',
    data: {
        QR: result.text
    },
    success: function(response) {
        $('#result').html(''); //clean the field
        alert("QR sent to server successfully");
        $("#result").append(response + '<br/>');
    },
    error: function() {
        alert('Not working!');
    }
});

服务器代码:接收QR扫描结果作为输入,然后使用输入从MySQL数据库检索记录,并立即在PHP页面上显示结果,虽然我的代码可以成功地从db检索记录,但ECHO显示在我的Android APP上,而不是我的服务器PHP界面。

我想达到像图书馆条码扫描结果的结果,是我的方法错了吗?

    <?php
      if(isset($_POST['QR'])){  //check is the QR result is empty or not
  $qr = $_POST['QR'];   //QR scanned result send from APP to server
  $tablename = "book";
  $db = mysql_connect("localhost", "root", "");
      if(!mysql_select_db("testing", $db)){
         print mysql_error();
  }
  if(!empty($qr)) {
    $sql="SELECT bk_title FROM ".$tablename." WHERE bk_id = '".$qr."'";
    $retval = mysql_query($sql, $db);
    if(! $retval){
        die("Cound not get data: ".mysql_error());
    }
    while($row = mysql_fetch_assoc($retval)){
        echo "Book Title :{$row['bk_title']} <br> ";
    }
    mysql_close($db);
  }     
  $reply = "Server Received";
  print json_encode($reply);
      }
    ?>

你在这里犯了几个错误。请将您的代码替换为以下代码:

$.ajax({        
            url         : "http://192.168.1.2/receiveQR.php",
            dataType: "json"
            type        : 'POST',
            data        : { QR : result.text},        
            success     : function(response){
                   $('#result').html(''); //clean the field
                   alert("QR sent to server successfully");             
                   $("#result").append(response.data + '<br/>');  
            },
            error       : function() {
                alert('Not working!');
            }
        });

<?php
 if(isset($_POST['QR']))
 {  
      //check is the QR result is empty or not
      $qr = $_POST['QR'];   //QR scanned result send from APP to server
      $tablename = "book";
      $db = mysql_connect("localhost", "root", "");
          if(!mysql_select_db("testing", $db)){
             print mysql_error();
      }
      if(!empty($qr)) {
      $reply=array();
        $sql="SELECT bk_title FROM ".$tablename." WHERE bk_id = '".$qr."'";
        $retval = mysql_query($sql, $db);
        if(! $retval){
            die("Cound not get data: ".mysql_error());
        }
        while($row = mysql_fetch_assoc($retval)){
            $reply['data'] = "Book Title :{$row['bk_title']} <br> ";
        }
        mysql_close($db);
      }     
      $reply['message'] = "Server Received";
      print json_encode($reply);
 }
 ?>

使用这个

javascript

formData = {
    QR: result.text
}
$.ajax({
    url: "http://192.168.1.2/receiveQR.php",
    type: 'POST',
    data: formData,
    success: function(response) {
        $('#result').html(''); //clean the field
        alert("QR sent to server successfully");
        $("#result").append(response + '<br/>');
    },
    error: function() {
        alert('Not working!');
    }
});
php

<?php
      if(isset($_POST['QR'])){  //check is the QR result is empty or not
  $qr = $_POST['QR'];   //QR scanned result send from APP to server
  $tablename = "book";
  $db = mysql_connect("localhost", "root", "");
      if(!mysql_select_db("testing", $db)){
         print mysql_error();
  }
  if(!empty($qr)) {
    $sql="SELECT bk_title FROM ".$tablename." WHERE bk_id = '".$qr."'";
    $retval = mysql_query($sql, $db);
    if(! $retval){
        die("Cound not get data: ".mysql_error());
    }
    while($row = mysql_fetch_assoc($retval)){
        echo "Book Title :{$row['bk_title']} <br> ";
    }
    mysql_close($db);
  }     
  $reply = "Server Received";
  print json_encode($reply);
      }
    ?>