Merge PHP code and HTML

Hi guys

I have an HTML page that has a form action which uses PHP. I would like to consolidate these two into one PHP index page. Is it possible?

Please find code below:

<html>

<h1 style="color:blue;">Title 1</h1>
<form action="htmltobash.php" method="get">
  Circuit: <select name="circuitsp"><option value="A">A</option><option value="B">B</option></select><br>
  Pod: <select name="podidsp"><option value="A">A</option><option value="B">B</option></select><br>
  Setpoint: <input type="text" name="setpoint"><br>
  <button type="submit" name="SubmitSetpoint" value="SubmitSetpoint">Set Setpoint</button>
<br>
<br>
<<h1 style="color:blue;">Title 2</h1>
<form action="htmltobash.php" method="get">
  Circuit: <select name="circuitps"><option value="A">A</option><option value="B">B</option></select><br>
  Pod: <select name="podidps"><option value="A">A</option><option value="B">B</option></select><br>
  Power: <select name="power"><option value="On">On</option><option value="Off">Off</option></select><br>
  <button type="submit" name="SubmitSetpower" value="SubmitSetpower">Set Power</button>

</form>
</html>

And the PHP

<?php
if (isset($_GET['SubmitSetpoint']) && $_GET['SubmitSetpoint'] == "SubmitSetpoint") {
        $field1 = $_GET["setpoint"];
        $field2 = $_GET["podidsp"];
        $field3 = $_GET["circuitsp"];
        echo "$field1 $field2 $field3";
        $output = shell_exec("/path/to.sh '$field1' '$field2' '$field3'");
        echo "<pre>$output</pre>";
}
elseif (isset($_GET['SubmitSetpower']) && $_GET['SubmitSetpower'] == "SubmitSetpower") {
        $field1 = $_GET["power"];
        $field2 = $_GET["podidps"];
        $field3 = $_GET["circuitps"];
        echo "$field1 $field2 $field3";
        $output = shell_exec("/path/to.sh '$field1' '$field2' '$field3'");
        echo "<pre>$output</pre>";

}

else
   exit


?>

It’s possible, I dont know if it is what you should do. Why are you executing a shell script?

Read that.

Starts with the code the way you say you want it.

Then improves it by splitting it up the way yours is already split into 2.

Then improves it some more by adding a 3rd file.

I don’t know a lot of PHP - I do know some bash.

I’ve recently developed this simple page to control a set of functions via SNMP - I did it 3 way:

HTML Page -> PHP -> Calls bash.

I understand that PHP should allow me to do the whole thing so now I’m trying to simplify it all.

Cheers - If i understand correctly he’s actually advocating keeping them split then.

Don’t confuse having one php index page, that may have the code needed to implement that page organized in multiple files or by using a framework, with having multiple pages.

What you are asking about is to put all the functionality on a single page. All you need to do to accomplish this is to have the form processing code and the html document containing the form(s) on the same page. For a simple web page and for where you are at in your learning cycle, start by putting these in one file.

Some notes:

  1. The form processing code needs to be above the start of the html document. This will allow you to display any validation errors when you re-display the form(s) and you also need to select/populate the form field values with any previously submitted data so that the user doesn’t need to keep selecting/typing in the information when there are validation errors. This organization will also let you edit existing data in addition to creating new data.

  2. If your form(s) are creating/modifying data or causing an action to be performed on the server, you should use post method form(s). If your form(s) are determining what data will be gotten and displayed on the web page, you should use get method form(s).

  3. After your code has detected if/which form has been submitted, you need to validate the input data before using it. You should use an array to hold validation error messages. The array is also an error flag. If the array is empty, there are no errors. If the array is not empty, there are errors. To display the errors in the html document, just display the content of the array.

  4. If the input data is valid (array holding error messages is empty) use the submitted data for whatever purpose you have designed the script to perform. For the case of a get method form, you should produce/retrieve the data needed to display the page and store it in appropriately named php variable(s). Simple code in the html document would use the data in the php variable(s) when producing the output on the web page.

  5. Any dynamic/unknown values (form field values, other external data, produced/retrieved data) that you output on the web page needs to be passed through htmlentities() to help prevent cross site scripting.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service