Can a result generated in PHP be displayed in an HTML?

0

I am doing a program that makes several calculations and in the end generates the result in a $resultado , the data is entered from the HTML in <form method="POST" action="archivo.php" >

For example, in the <form> , the user enters:

Value 1 + Value 2

They are sent to file.php by means of a button to be added and the result is output in $resultado

And what I want is that on the HTML page (even if you have to reload the page) show something like:

"Your value equals 5"

Where 5 is what is left in $resultado .

What I wanted to know is if it can be done and if so, how to show the result in the HTML or if it is more convenient to do it all in a PHP.

    
asked by CarlosDayan 23.06.2017 в 01:13
source

2 answers

1

file.html:

<form method="post" action="archivo.php">
    <input name="valor1">
    <input name="valor2">
</form>

archivo.php:

If you only want to print it:

<?php
    $valor1 = filter_input(INPUT_POST, 'valor1');
    $valor2 = filter_input(INPUT_POST, 'valor2');
    $resultado = $valor1 + $valor2;
    echo "Tu valor es igual a $resultado";

If you want to replicate the initial form, to make a new calculation maybe:

<?php
    $valor1 = filter_input(INPUT_POST, 'valor1');
    $valor2 = filter_input(INPUT_POST, 'valor2');
    $resultado = $valor1 + $valor2;
?>

<form method="post" action="archivo.php">
    <input name="<?php echo $valor1 ?>">
    <input name="<?php echo $valor2 ?>">
    <span><?php echo "Tu valor es igual a $resultado"; ?>
</form>

Remember that you can mix PHP code in HTML content, as expressed by php.net:

  

PHP pages contain HTML with embedded code that does "something"

I leave the link for you to read the full content: link

    
answered by 23.06.2017 в 01:43
0

If you can what you comment, what you have to do is that your file instead of saving it as .hmtl save it as .php .

Then in file.php put something like the following:

<?php
    $valor1 = $_POST["valor1"];
    $valor2 = $_POST["valor2"];
    $resultado = $valor1 + $valor2;
?>
<html>
<body>

<p>El resultado es <?php echo $resultado ?>.</p>

</body>
</html>
    
answered by 23.06.2017 в 01:38