1

嗨,我正在尝试从一个程序运行两个 PHP 函数,这些函数通过按两个 html 表单输入来执行,一个带有 2 个按钮的表格,开始和删除;开始写入文件“打开”,删除写入文件“关闭”,但是当我单击运行中的两个按钮时,两个输出都关闭的 php 脚本请帮助!


有按钮的表。

<html>
<TABLE BORDER="3" CELLPADDING="0" CELLSPACING="10">
<TD>
<table BORDER="3" CELLPADDING="3" CELLSPACING="3">
           <TH>Socket 1</th>
           <TR></tr>
           <TD>Serial Number</TD> <TD>ON/OFF</TD>
           <TR></TR>
           <TD>Elapsed Time </TD> 
           <TD><?php
                 include_once ("statusfileon.php")         
                     ?></TD> 
           <TD><?php 
                 include_once ("statusfileoff.php")         
                     ?></td>
           <TR></TR>
           </TABLE>
</TD>
<TD>
<table BORDER="3" CELLPADDING="3" CELLSPACING="3">
           <TH>Socket 2</th>
           <TR></tr>
           <TD>Serial Number</TD> <TD>ON/OFF</TD>
           <TR></TR>
           <TD>Elapsed Time</TD> <TD>Start</TD> <TD>Remove</td>
           <TR></TR>
           </TABLE> 
</TD>
</TABLE>
</html>

Satusfileon.php

<?php // statusfileon.php
      //Write file if button is clicked
      if(isset($_POST['submit'])) {
               statusfileon();
}
?>
<?php //Write file function
function statusfileon(){ //File function name.
$fh = fopen("statusfile.txt", 'w') or die("Fail to create file");
//Text which is displayed in file (on).
$text = <<<_END
on
_END;
fwrite($fh, $text) or die("Could not write to file"); // If file location could not be found file isn't writen.
fclose($fh); //Close file.
}
?>
<html>
   <!--Button and variable for file permission. -->
    <form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
     <input type="submit" name="submit" value="Start">
      </form>
</html>

状态文件off.php

<?php // statusfileoff.php
      //Write file if button is clicked.
      if(isset($_POST['submit'])) {
               statusfileoff();
}
?>
<?php //Write file function 
function statusfileoff(){ //File function name.
$fh = fopen("statusfile.txt", 'w') or die("Fail to create file");
//Text which is displayed in file (off).
$text = <<<_END
off 
_END;
fwrite($fh, $text) or die("Could not write to file"); // If file location could not be found file isn't writen.
fclose($fh); //Close file.
}
?>
<html>
   <!--Button and variable for file permission. -->
    <form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
     <input type="submit" name="submit" value="Remove">
      </form>
</html>

任何帮助将不胜感激,因为我是编程新手,请原谅任何不良做法,谢谢

4

1 回答 1

2
  <TD><?php
       include_once ("statusfileon.php")         
  ?></TD> 
       <TD><?php 
             include_once ("statusfileoff.php")         
  ?></td>

在提供 HTML之前,服务器会执行 PHP 。因此,单击按钮并不像调用您的 PHP 函数。

在这种情况下,statusfileon()执行statusfileoff(),然后立即执行,然后将页面提供给您的浏览器。按钮点击没有做任何事情。


要执行代码,如果您想要您正在寻找的行为,您可能希望使用 HTML 链接将用户重定向到statusfileon.phpstatusfileoff.php单击按钮。

于 2012-11-02T11:25:41.760 回答