2017-09-24 259 views
1

HTML通讯我非常新手在编程的这个领域,因此任何指导,将不胜感激。我为我的Rasp Pi提供了一个相机流,我只是附加了一个伺服器来转动它。我已将问题分为三个阶段。PHP /为树莓派

1)HTML GUI为一个简单的两个按钮,具有用于照相机度的值框。

2)PHP纸条读值每一个按钮被按下的HTML的时间和它写下来到一个文本文件中。

3)Python脚本读取的文本文件,并相应地移动相机。

我已经完成步骤3,其围绕转动伺服。我还在html中创建了一个小的GUI作为步骤1。我主要关心的是将客户端的HTML页面链接到PHP服务器端。我的专业知识在这里结束。我有一个托管在我的Pi上的网站,所以我只需要通过简单的网页即可控制摄像头的位置。有没有一种简单的方法可以与PHP交谈?如果我最初可以从html按钮获取数据,我可以编写一个简单的php代码将值写入文本文件。

而准确的说,该按钮不会被传递的实际数目,只是一个递增/递减信号,以在PHP其中具有增加或减少(在相机度角)中的硬编码值。

这是我的代码到目前为止。

<!doctype html> 
<html> 
<head> 
    <title>Camera Control</title> 
</head> 
<body> 
<h1 style="text-align: center;">Camera Control</h1> 

<p style="text-align: center;"> 
    <input name="Leftbutton" type="button" value="Left" />&nbsp;&nbsp; 
    <input maxlength="5" name="textbox" size="5" type="text" />&nbsp;&nbsp; 
    <input name="Rightbutton" type="button" value="Right" /> 
</p> 
</body> 
</html> 

Preview of the HTML


<?php 
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); 
$txt = " **Some value from the button, text or number** \n"; 
fwrite($myfile, $txt); 
fclose($myfile); 
?> 

我的目标是要避免的JavaScript或语言,我不知道,如果有一个简单容易的方法,我可以完成这件事将是惊人的。提前致谢。

+1

搜索'PHP的形式handling'如无物正在发布。 – Xorifelse

+0

似乎正确的想法让我走了,非常感谢! –

回答

0

下面这段代码应该是index.php文件:

<?php 
    if($_GET['return'] == 'failed'){ 
    echo '<p>unable to execute command.<p>'; 
    } 
?> 

<style> 
    #camera > input{ 
    margin-left 10px; 
    } 
</style> 
<form id="camera" action="camera.php" method="post"> 
    <input type="submit" name="left" value="left"> 
    <input maxlength="5" name="textbox" size="5" type="text" /> 
    <input type="submit" name="right" value="right"> 
</form> 

那么对于PHP是这样的:

<?php 
    #camera.php 

    if(isset($t = $_POST['textbox']) && isset($l = $_POST['left']) || isset($r = $_POST['right'])){ 
    if(isset($l){ 
     $v = "left"; 
    } elseif(isset($r)) { 
     $v = "right"; 
    } else { 
     header('Location: ./index.php?return="failed"'); 
     die(); 
    } 

    if(!file_put_contents('data.txt', time() . "| $v:$t" . PHP_EOL)){ 
     header('Location: ./index.php?return="failed"'); 
     die(); 
    } 

    header('Location: ./index.php'); 

    // if access to shell 
    exec('python ./location/to/yourscript.py'); 
    } 

?> 
0
<html> 
<head> 
    <title>PHP Test Page 
    </title> 
</head> 


<body> 
<form method="post" action="temp.php"> 
<input type="submit" value="Left" name="leftb"> 
<input type="submit" value="Right" name="rightb"> 
</form> 



</body> 
</html> 




<?php 
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); 

if ($_POST['leftb']) 
{ echo "Left is pressed"; 
    $txt = -1;} 


else if ($_POST['rightb']) 
{ echo "Right is pressed"; 
    $txt = 1;} 


fwrite($myfile, $txt); 
fclose($myfile); 



sleep(1); 

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); 
$txt = 0; 
fwrite($myfile, $txt); 

fclose($myfile); 

?> 
+0

这就是我设法让它工作的原因! –