服务器从客户端电话接收QR扫描结果后,如何在php页面上显示查询结果

时间:2014-03-21 09:56:12

标签: php android mysql cordova qr-code

我正在使用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);
      }
    ?>

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

使用这个

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