dongran1779 2018-06-09 03:42
浏览 175

将Python输出发送到HTML字段

I'm currently doing a simple project using a Raspberry Pi. I have a simple Apache 2 server running, with MySQL and few PHP/HTML pages. One of the pages is the one below call barcode.php. On the barcode.php you enter a User_ID on the user_id field and press enter or press the find button and it will search in the database for that user and display that user information and picture.

All of the above works manually and even with a barcode scanner by scanning a QR code. Now, I want to integrate an RFID reader (RFID READER RC522). When the user scans his/her card on the reader I want it to work like the barcode scanner and input the numeric code in the user_id field and press enter so the page can return the user data.

I'm very new to this. How can I implement this into my barcode.php? Where a user scans, and it will put it on the user_id field.

Here's the 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>

Here's the simple Python script called Read.py

#!/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)

On the terminal, when scanning a card it returns the following data:

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

I want to send that numeric data (140248588882) to the user_id field in my barcode.php page. How can I accomplish this?

  • 写回答

3条回答 默认 最新

  • douzhuangna6906 2018-06-09 04:36
    关注

    You need a client to punch text into the web browser, which is not a PHP problem.

    Typically a barcode scanner will act as a HID, so it appears to type out the number and press enter wherever you scan something. You can either plug in an alernate RFID reader that acts as a HID, or simulate keyboard events from python.

    • Sort out permissions or pass around the data so that you can get the RFID input without sudo: To create the keyboard events, you should be running under your graphical session, as yourself.
    • Call the something like xte from python, which is part of the Debian xautomation package.
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)