页面自动刷新后出现意外的表单提交结果

时间:2018-12-28 11:18:25

标签: php html

我有一个带有复选框的简单表格。提交后,它执行外部(Python)命令,该命令在外部硬件(RPi)上切换继电器(0/1)。可以使用其他外部(Python)命令读取开关(继电器)的状态。表单本身运行良好,但我无法解决某些问题: 1.提交加厚复选框后,复选框打勾消失(外部切换命令成功运行) 2.选中首页自动刷新复选框后,它将再次运行带有“ 0”参数的外部命令。 3.在第二页的自动刷新复选框取消选中后,它从外部硬件获得“ 0”。 我想在提交后保持选中/取消选中状态,并且在任何页面刷新时都不会相应地切换回去。我想不通几天的原因。

<?php 
$get_vizf = 'sudo python -c \'import sys; sys.path.insert(0, "/home/pi/Documents/medence"); from PHP_exec import *; print 
gpio_read("VIZFORGATO")\''; //Command for reading external switch (relay) state
$cgpio_set = 'sudo python -c \'import sys; sys.path.insert(0, "/home/pi/Documents/medence"); from PHP_exec import *; gpio_set'; //Here comes $ext var to complete the command
$x = shell_exec($get_vizf)
?>

<html>
<meta http-equiv="refresh" content="15">
<body>
<form action="/test.php" name='toggles' id='toggles' method="post">
<div>
<p><span style="font-size: 20px;">Checkbox</span></p>
<p>
<input type="checkbox" id="s_vizf" name="onoffsw_vizf" value="ON" <?php 
if($x==1){echo "checked";} ?>>
</p>
<p>
<?php 
echo "Before:<br>";
  echo "ISSET_init:".isset($_POST['onoffsw_vizf'])."<br>"; //Checkbox state before submit
  if(!empty($_POST['onoffsw_vizf'])) //Setting up requested switching command (1 if chekbox ticked 0 if not)
      {$ext = '("VIZFORGATO",1)\'';} //Setting up requested switching command (1 if chekbox ticked 0 if not)
  if(empty($_POST['onoffsw_vizf'])) //Setting up requested switching command ((1 if chekbox ticked 0 if not)
      {$ext = '("VIZFORGATO",0)\'';} //Setting up requested switching command (1 if chekbox ticked 0 if not)
  $state_0 = shell_exec($get_vizf); //Reading external switch (relay) state before submit  
  echo "G_init:".$state_0."<br>"; //GPIO állapot előtte
  shell_exec($cgpio_set.$ext); //Switching an external switch on the server (0 or 1)
  $state_1 = shell_exec($get_vizf); //Reading external switch (relay) state after submit
echo "<br>After:<br>";
  echo "G_end:".$state_1."<br>"; //GPIO állapot utána         
  echo "ISSET_end:".isset($_POST['onoffsw_vizf'])."<br>";//Checkbox state before submit
    ?>
</p>
<p>    
<input class="submit_button" type="submit" name="submit_button" id="submit_button" value="Submit">    
</p>
</div>
</form>
</body>
</html>

0 个答案:

没有答案
相关问题