将Python输出发送到HTML字段

时间:2018-06-09 03:42:57

标签: php python html raspberry-pi

我目前正在使用Raspberry Pi进行一个简单的项目。我有一个简单的Apache 2服务器运行,有MySQL和很少的PHP / HTML页面。其中一个页面是下面的一个名为barcode.php的页面。在barcode.php上,在user_id字段中输入User_ID,然后按Enter或按查找按钮,它将在数据库中搜索该用户并显示该用户信息和图片。

以上所有工作都是通过扫描QR码手动甚至使用条形码扫描仪。现在,我想整合RFID阅读器(RFID READER RC522)。当用户在阅读器上扫描他/她的卡时,我希望它像条形码扫描器一样工作,并在user_id字段中输入数字代码,然后按回车键,这样页面就可以返回用户数据。

我对此很新。我怎样才能将它实现到我的barcode.php中?用户扫描的位置,并将其放在user_id字段上。

这是barcode.php:

    <?php
        // Initialize the variables
        $user_id   = "";
        $osha      = "";
        $firstname = "";
        $lastname  = "";
        $company   = "";
        $trade     = "";
        $email     = "";
        $picture   = "";
        $reg_date   = "";

        // PHP code to search data in a MySQL database and set it in input text
        if(isset($_POST['search']))
        {
            // Connect to MySQL
            $dbc = mysqli_connect("127.0.0.1", "root", "1234", "demodb");

            // id to search
            $user_id = mysqli_real_escape_string($dbc, $_POST['user_id']);

            $query = "SELECT * FROM Users WHERE user_id = '$user_id' LIMIT 1";
            $rs    = mysqli_query($dbc, $query);
            if (mysqli_num_rows($rs) == 1)
            {
                $row       = mysqli_fetch_array($rs);
                $osha      = $row['osha'];
                $firstname = $row['firstname'];
                $lastname  = $row['lastname'];
                $company   = $row['company'];
                $trade     = $row['trade'];
                $email     = $row['email'];
                $picture   = $row['picture'];
                $reg_date  = $row['reg_date'];

                $query1     = "INSERT INTO scan (user_id, osha, firstname, lastname, company, trade, email, picture) VALUES (" .
                        "'" . $user_id . "'," .
                      "'" . mysqli_real_escape_string($dbc, $osha     ) . "', " .
                      "'" . mysqli_real_escape_string($dbc, $firstname) . "', " .
                      "'" . mysqli_real_escape_string($dbc, $lastname ) . "', " .
                      "'" . mysqli_real_escape_string($dbc, $company  ) . "', " .
                      "'" . mysqli_real_escape_string($dbc, $trade  ) . "', " .
                      "'" . mysqli_real_escape_string($dbc, $email  ) . "', " .
                      "'" . mysqli_real_escape_string($dbc, $picture    ) . "')";
                        mysqli_query($dbc, $query1);
            }
            else
            {
                echo "Undefined ID";
            }
        }
    ?>

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>User Search</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!--===============================================================================================-->
                <link rel="icon" type="image/png" href="images/icons/favicon.ico"/>
        <!--===============================================================================================-->
                <link rel="stylesheet" type="text/css" href="vendor/bootstrap/css/bootstrap.min.css">
        <!--===============================================================================================-->
                <link rel="stylesheet" type="text/css" href="fonts/font-awesome-4.7.0/css/font-awesome.min.css">
        <!--===============================================================================================-->
                <link rel="stylesheet" type="text/css" href="vendor/animate/animate.css">
        <!--===============================================================================================-->
                <link rel="stylesheet" type="text/css" href="vendor/css-hamburgers/hamburgers.min.css">
        <!--===============================================================================================-->
                <link rel="stylesheet" type="text/css" href="vendor/select2/select2.min.css">
        <!--===============================================================================================-->
                <link rel="stylesheet" type="text/css" href="css/util.css">
                <link rel="stylesheet" type="text/css" href="css/main.css">
        <!--===============================================================================================-->

        <style>
            .button {
                background-color: #4CAF50;
                border: none;
                color: white;
                padding: 15px 32px;
                text-align: center;
                text-decoration: none;
                display: inline-block;
                font-size: 16px;
                margin: 4px 2px;
                cursor: pointer;
            }
        </style>
    </head>

    <body>
        <div class="contact1">
            <div class="container-contact1">
                <div class="contact1-pic js-tilt" data-tilt>

                    <?php echo "<img src= '$picture' height='400' width='400' alt='Image not found' onerror=this.onerror=null;this.src='images/scanbarcode.png';  >" ?>

                </div>

                <form action="barcode.php" method="post">
                    <div class="wrap-input1 validate-input" data-validate = "SCAN ID TO SEARCH">
                    <input class="input1" type="text" name="user_id" placeholder="SCAN ID TO SEARCH" autofocus>
                    <span class="shadow-input1"></span>
            </div>

            <div class="wrap-input1 validate-input" >
                <input class="input1" type="text" name="osha" placeholder="OSHA #" value="<?= htmlspecialchars($osha) ?>">
                <span class="shadow-input1"></span>
            </div>

            <div class="wrap-input1 validate-input" >
                <input class="input1" type="text" name="firstname" placeholder="First Name"  value="<?= htmlspecialchars($firstname) ?>">
                <span class="shadow-input1"></span>
            </div>

            <div class="wrap-input1 validate-input" >
                <input class="input1" type="text" name="lastname" placeholder="Last Name"  value="<?= htmlspecialchars($lastname) ?>">
                <span class="shadow-input1"></span>
            </div>

            <div class="wrap-input1 validate-input" >
                <input class="input1" type="text" name="company" placeholder="Company Name"  value="<?= htmlspecialchars($company) ?>">
                <span class="shadow-input1"></span>
            </div>

            <div class="wrap-input1 validate-input" >
                <input class="input1" type="text" name="trade" placeholder="Trade Name"  value="<?= htmlspecialchars($trade) ?>">
                <span class="shadow-input1"></span>
            </div>

            <input type="button" onclick="window.location='auth.php'" class="button" value="Admin"/>

            <input type="submit" name="search" value="Find" class="button" >

            </form>

        </div>
    </div>

    <!--===============================================================================================-->
            <script src="vendor/jquery/jquery-3.2.1.min.js"></script>
    <!--===============================================================================================-->
            <script src="vendor/bootstrap/js/popper.js"></script>
            <script src="vendor/bootstrap/js/bootstrap.min.js"></script>
    <!--===============================================================================================-->
            <script src="vendor/select2/select2.min.js"></script>
    <!--===============================================================================================-->
            <script src="vendor/tilt/tilt.jquery.min.js"></script>

            <script >
                    $('.js-tilt').tilt({
                            scale: 1.1
                    })
            </script>
    </body>
    </html>

这是一个名为Read.py的简单Python脚本

#!/usr/bin/env python

import RPi.GPIO as GPIO
import SimpleMFRC522
import MFRC522
import signal

continue_reading = True

reader = SimpleMFRC522.SimpleMFRC522()

def end_read(signal,frame):
    print "Ctrl+C captured, ending read."
    continue_reading = False
    GPIO.cleanup()

signal.signal(signal.SIGINT, end_read)

MIFAREReader = MFRC522.MFRC522()

while continue_reading:
    id, text = reader.read()
    print(id)
    print(text)

在终端上,扫描卡时会返回以下数据:

pi@raspberrypi:/var/www/html/app/RFID/SPI-Py/MFRC522-python $ sudo python Read1.py
140248588882

我想将数字数据(140248588882)发送到barcode.php页面的user_id字段。我怎么能做到这一点?

3 个答案:

答案 0 :(得分:1)

您需要客户端将文本打入Web浏览器,这不是PHP问题。

通常条形码扫描器将充当HID,因此它似乎输入数字并在您扫描某物的任何地方按下输入。您可以插入充当HID的alernate RFID阅读器,或者从python模拟键盘事件。

  • 排序权限或传递数据,以便您可以在不使用sudo的情况下获取RFID输入:要创建键盘事件,您应该像您一样在图形会话下运行。
  • 从python中调用类似xte的内容,这是Debian xautomation包的一部分。

答案 1 :(得分:0)

这是一个快速入侵,但你可以创建一个这样的PHP页面:

<?php
  print `sudo python Read1.py`;

然后在主PHP页面中使用JavaScript来轮询该PHP脚本,如果/当它返回有效ID时,将其插入输入并提交表单。

你应该这样做,以便Read1.py可以在没有sudo的情况下运行(让PHP能够运行无密码的sudo是一个安全问题)。

您还需要处理轮询超时的情况,在这种情况下,JavaScript应该再次轮询。

答案 2 :(得分:0)

您的问题是您需要一个Python文件才能将条形码发送到PHP脚本。

在Python脚本中添加以下代码:

DWORD nNumberOfBytesRead;
...
if (ReadFile(file, lpBuffer, nNumberOfBytesToRead-1, &nNumberOfBytesRead, NULL))

但是,您需要在barcode.php中将from webbrowser import open as start_webpage ... ... ... ... if barcode_scanned: start_webpage(url + "?user_id=" + str(barcode)) 作为GET参数:

user_id