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

标签 php android mysql cordova qr-code

我正在使用 PhoneGap 创建一个 Android 二维码扫描应用程序,该应用程序充当客户端,而服务器则使用 PHP 和 MySQL (WAMP)。下面是二维码扫描应用程序将扫描结果发送到服务器的部分,它设法发送并从服务器获取回复,所以我认为问题出在下一节中的服务器代码

$.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!');
    }
});

服务器代码:接收二维码扫描结果作为输入,然后使用输入从MySQL数据库检索记录,立即将结果显示在PHP页面上,虽然我的代码可以成功从数据库检索记录,但ECHO显示在我的 Android 应用程序,而不是我的服务器 PHP 接口(interface)。

我想要达到像图书馆条形码扫描的结果,我的方法是否错误?

    <?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);
 }
 ?>

关于php - 服务器接收到作为二维码扫描器的客户端手机的二维码扫描结果后,如何将查询结果显示在php页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22555461/

相关文章:

android - 使用输入对象的 graphql 突变

php - 根据数组按顺序获取数据

php - 无法在我的导航栏的下拉菜单中传递 id

android - ionic 构建安卓 Nodejs

php - Magento 分层导航 - 复选框

mysql - SQL - 如何计算表1中的订阅数量并连接表2中的数据

php - 数据库 |来自两个表的条​​件查询

php - php mysql 标题中的列名

php - 使用 PHP 在 MySQL 中插入和检索日期的正确方法

java - 如何将图像从android发送到servlet然后存储