首页 > 解决方案 > 通过 href 方法将复选框的值发送到 control.php 文件

问题描述

我的网络摄像头有一个 control.php 文件,我可以使用图像映射的 href 链接向它发送命令,如下所示:href="command.php?cmd=1"。在 control.php 中有一个睡眠定时器,它会在 1 秒后停止相机的移动。一切正常。现在,我想在网络摄像头流媒体页面中添加一个复选框,并且除了命令之外,还希望将复选框的值发送到 command.php。它会在一秒钟后停止凸轮的移动,取决于复选框的值。我在 php 甚至 JS 中都尝试过,但我太愚蠢了,无法完成。非常感谢任何帮助!干杯,福尔克

标签: phpcheckboxparametershref

解决方案


您可以在复选框上使用 javascript 事件 onclick() 并对第二个 PHP 脚本进行 AJAX 调用。随着this您将复选框对象及其数据作为参数传递。

<input type='checkbox' onclick='moveCamera(this);'>

<script>
    function moveCamera(checkbox) {
       var http = new XMLHttpRequest();
       var url = 'command.php';
       var params = 'cmd=1&checked=' + checkbox.checked;

       http.open('POST', url, true);
       http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
       http.onreadystatechange = function() {
           if(http.readyState == 4 && http.status == 200) {
           // Here you can put code that will be executed after the call is complete
           }
       }
       http.send(params)
    }
</script>

现在您可以checkedcmd在第二个 php 脚本中一样访问。


推荐阅读