Karan
Karan

Reputation: 51

How to get form output value on same HTML form page?

I am looking to submit a HTML form file (home.html) with PHP action file (action.php) to display the output on same home.html file after processing through action.php.

I got many scripts solving this problem with .php file, Is it possible to do the same with HTML?

home.html

<form action="action.php" method="post">
Enter Numeric Value: <input type="number" name="num">
<input type="Submit" value="Calculate">
</form>

action.php

<?php 
    $a=$_POST["num"];
    echo "Contact our sales person for higher volume of Emails";
?>

Upvotes: 2

Views: 5745

Answers (2)

bloodyKnuckles
bloodyKnuckles

Reputation: 12079

Another option, first load the HTML file, then pull it into the PHP file:

home.html:

<form action="action.php" method="post">
Enter Numeric Value: <input type="number" name="num" value="<?=$a?>">
<input type="Submit" value="Calculate">
</form>

Notice the "value" field and the php code inserted there.

action.php:

<?php
    $a=$_POST["num"];
    echo "Contact our sales person for higher volume of Emails";

    require 'home.html';
?>

Simply require'ing the home.html file.

First you load home.html in your browser, enter a number, submit the form to action.php. Then when action.php loads, it displays the appropriate message, pulls in the home.html page, and loads the variable $a into the form field. When loaded as a plain HTML file, the PHP code is ignored by the browser, but parsed when the action.php file require's the HTML file.

Upvotes: 1

Fluffeh
Fluffeh

Reputation: 33512

Modify your home file into a PHP file and point the form action to it, then you can simply use the POST/GET data and display it on the page:

I have added the use of "htmlentities" to more it a little more secure

home.php

<?php

    if(isset($_POST['num']))
    {
        echo htmlentities($_POST['num']);
    }

?>

<form action="home.php" method="post">
Enter Numeric Value: <input type="number" name="num">
<input type="Submit" value="Calculate">
</form>

Upvotes: 1

Related Questions