简体   繁体   English

如何使用切换按钮?

[英]How do I use a toggle button?

I'm working on an Arduino project that uses the Ethernet shield. 我正在使用以太网屏蔽的Arduino项目上工作。 The Adruino connects to my laptop (server) and reads a PHP file telling it to turn on or off a relay. Adruino连接到我的笔记本电脑(服务器)并读取一个PHP文件,告诉它打开或关闭继电器。

The user interface is an HTML page. 用户界面是HTML页面。 There are submit forms with values on and off. 有些提交表单的值处于打开和关闭状态。 The values get sent to a handler page for processing so that it can be read by the Arduino. 这些值将发送到处理程序页面进行处理,以便可以由Arduino读取。

I want to make use of a CSS/JavaScript-styled toggle button to replace the hideous looking submit buttons. 我想利用CSS / JavaScript样式的切换按钮来代替看起来丑陋的提交按钮。

http://proto.io/freebies/onoff/ is the button I want to use. http://proto.io/freebies/onoff/是我要使用的按钮。 How do I extract the values of the toggle states and implement it on my HTML page? 如何提取切换状态的值并在HTML页面上实现它?

1 Store the status of your system somehow 1 以某种方式存储系统状态

You can use a database, a file, a service for this. 您可以为此使用数据库,文件,服务。

2 The Arduino should connect to your server via a PHP script 2 Arduino应该通过PHP脚本连接到服务器

This script is returns the status of the system to whatever value is set 该脚本将系统的状态返回到设置的任何值

3 Create the user interface 3 创建用户界面

This can be another PHP script. 这可以是另一个PHP脚本。 It should display your button. 它应该显示您的按钮。

4 Use jQuery to capture the click event. 4 使用jQuery捕获click事件。

Then fire an Ajax call to the server and change the status of the system, the server should return the new status, use $.ajax success callback function to check for the new status and update your user interface (you have to do this again in case the status could not be persisted you prevent your UI to go out of sync) 然后向服务器发出Ajax调用并更改系统状态,服务器应返回新状态,使用$.ajax成功回调函数检查新状态并更新用户界面(您必须在如果状态无法保持,您可以防止UI不同步)

Well, to use JavaScript would entail you to use Ajax which I'm not familiar with at this point. 好吧,要使用JavaScript,您将需要使用Ajax,我对此并不熟悉。 I'd also recommend using writing to a text file than using a PHP file by the Arduino. 与使用Arduino的PHP文件相比,我还建议使用写入文本文件的方法。 Trying to be protective, would it not be possible to send request to the Arduino like you would any server using PHP APIs? 尝试保护性,是否不可能像使用PHP API的任何服务器一样向Arduino发送请求?

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://192.168.0.5');//depending on your arduino's address
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST['on/off']);
    //You may want to validate your variables.
?>

But I'm unsure whether the Arduino can read the send variable. 但是我不确定Arduino是否可以读取send变量。 If it works, the variable should appear in the serial if I'm right? 如果可以的话,如果我正确的话,变量应该出现在序列中?

Further website information. 更多网站信息。

Using a form is your best option. 最好使用表格。 To use a toggle you could use JavaScript code to post the form onClick. 要使用切换,您可以使用JavaScript代码发布onClick表单。

<form name="toggleForm" action="poster.php" method="POST"> <!-- Post method to get your variables. poster.php will be the script above.-->
    <input id="on" name="on/off" onClick="send()" value="on" type="radio"/>
    <input id="off" name="on/off" onClick="send()" value="off" type="radio"/>
</form>

javascript to submit the form onClick: javascript提交表单onClick:

<script>
    function send(){
        document.toggleForm.submit();
    }
</script>

CSS to make it look pretty: 使它看起来很漂亮的CSS:

<style>
    #on{background:#00FFFF;content:'On';}
    #off{background:#00FFFF;content:'Off';}
</style>

Xocoatzin 's path to the solution is good. Xocoatzin的解决方案是好的。 But if you like a pure CSS solution here is the DEMO . 但是,如果您喜欢纯CSS解决方案,则这里是DEMO

There is a hidden input type checkbox : 有一个隐藏的input类型checkbox

<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="onoffswitch1" checked="">

It can be used to get the value on the server side as well as by other scripts. 它可以用于获取服务器端以及其他脚本中的值。

You'll need to make a JavaScript Ajax event handler for the toggle button click. 您需要为切换按钮单击创建一个JavaScript Ajax事件处理程序。 The Ajax request should go to a PHP page which will then toggle / set the button state. Ajax请求应该转到PHP页面,然后将切换/设置按钮状态。 You would need some sort of way to store the state of the button, perhaps just a file? 您将需要某种方式来存储按钮的状态,也许只是一个文件? There is no need for anything too complex. 不需要太复杂的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM