Hi I’m trying to run two PHP functions from one program which are executed by pressing two html form inputs, A table with 2 buttons, Start and Remove; Start writes a file ‘ON’ and Remove writes a file ‘OFF’ but at the moment when i click both of the buttons in runs the php scripts with both out puts as OFF please Help!
The table with the buttons.
<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>
statusfileoff.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>
Any help would gladly be appreciated as I’m new to programming please excuse any bad practices
thanks
PHP is executed by the server before the HTML is served up. So it isn’t like clicking the buttons is calling your PHP function.
In this case,
statusfileon()is executed and right afterwards,statusfileoff()is executed and then the page is served to your browser. Button clicks aren’t doing anything.To execute the code, you might want to redirect the user to
statusfileon.phporstatusfileoff.phpon click of the button using an HTML link if you want the behaviour you are looking for.