将 jscolor 结果提交给 shell_exec 命令

Submitting jscolor result to shell_exec command

首先:我的网络编码技能几乎不存在,我只是在 HTML 年前做了一点...... 我与 php 的第一步是今天才迈出的。

我的目标是:拥有颜色选择器并将选择的值作为参数发送到 python 脚本。

我成功地使用了 jscolor picker 库 (http://jscolor.com/examples/),而且我还能够 运行 python 带有来自 [=28] 的(颜色)参数的脚本=].

我现在的问题是:

如何将颜色字符串 (hex-str) 提交给 exec 命令 (LED_color)?

<?php
    $LED_color="FFFFFF";

    $LED_color=escapeshellarg($LED_color);

    if (isset($_POST['button']))
    {
        $LED_color = $_REQUEST['hex-str'];
        echo shell_exec("sudo /home/pi/LEDscripts/color-by-arg.py $LED_color");
    }
?>


<html>
<head>
    <title>Basic usage</title>
</head>
<body>

<div style="position:absolute; left:280px; top:10px;">
    toString = <span id="hex-str"></span><br />
</div>


<script src="jscolor.js"></script>

Color: <input class="jscolor {closable:true,closeText:'Close me!',onFineChange:'update(this)'}" value="000000">

<script>
function update(picker) {
    document.getElementById('hex-str').innerHTML = picker.toString();
}
</script>

<form method="post">
    <p>
        <button name="button">Submit color</button>
    </p>
</form>

</body>
</html>

您的颜色选择器输入需要一个 name 属性来匹配您的 PHP 代码,并且还需要在 <form> 元素内。

<?php
if (!empty($_POST["hex-str"])) {
    $LED_color = escapeshellarg($_POST["hex-str"]);
    echo shell_exec("sudo /home/pi/LEDscripts/color-by-arg.py $LED_color");
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Basic usage</title>
    <script src="jscolor.js"></script>
</head>
<body>
    <form method="post">
        <p>
            Color:
            <input type="text" name="hex-str" value="000000" class="jscolor" data-jscolor="{closable:true,closeText:'Close me!',onFineChange:'update(this)'}"/>
            toString = <span id="hex-str-display"></span>
        </p>
        <p>
            <button type="submit">Submit color</button>
        </p>
    </form>
    <script>
        function update(picker) {
            document.getElementById('hex-str-display').innerHTML = picker.toString();
        }
    </script>
</body>
</html>