2

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";
?>
1
  • 2
    "to display the output on same home.html file" - You can't do that, unless you instruct Apache to treat .html files as PHP. Otherwise, you'd need to use an iframe, or rename your .html to .php Commented May 22, 2014 at 13:28

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

9 Comments

@Fluffeh: I know the things you suggested, but i was curious to know, is it possible same thing we can do with HTML only. thanks for the answer
@user3665127 you can also use javascript and ajax to do this
@user3665127 In HTML, No. You can however embed JS/Ajax and do it if you cannot use PHP, but the PHP solution is the easiest by far.
@Fluffeh Unless you instruct Apache to treat .html files as PHP, as per my comment under OP's question.
@Catalin Deaconescu: Hi i got it using simple Ajax and JS file. I just pass the php result to ajax file, that's included in html file. thanks for valuable suggestion.
|
1

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.

7 Comments

Are you sure about this? Do re-think this and read some of the comments under OP's question and the other answer.
That -1, it's mine. Re-read my comment above. Especially this comment
@Fred -ii- Cool, comments reread. What particularly do you think is a problem with this option? I've used this technique now and then, and just tested it to be doubly sure. PHP requires HTML files just fine and even parses the php code.
You want the OP to use value="<?=$a?>"> using .html as the extension? You say it worked for you... why? I know the why, I just want you to tell me.
From the manual: "When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags." php.net/manual/en/function.include.php
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.