[php] set value of input field by php variable's value

I have a simple php calculator which code is:

<html>
    <head>
        <title>PHP calculator</title>
    </head>

    <body bgcolor="orange">
        <h1 align="center">This is PHP Calculator</h1>
        <center>
            <form method="post" action="phptest.php">
                Type Value 1:<br><input type="text" name="value1"><br>
                Type value 2:<br><input type="text" name="value2"><br>
                Operator:<br><input type="text" name="sign"><br>
                Result:<br><input type"text" name="result">
                <div align="center">
                    <input type="submit" name="submit" value="Submit">
                </div>
            </form>
        </center>

<?php
    if(isset($_POST['submit'])){
        $value1=$_POST['value1'];
        $value2=$_POST['value2'];
        $sign=$_POST['sign'];

        if($value1=='') {
            echo "<script>alert('Please Enter Value 1')</script>";
            exit();
        }

        if($value2=='') {
            echo "<script>alert('Please Enter Value 2')</script>";
            exit();
        }

        if($sign=='+') {
            echo "Your answer is: " , $value1+$value2;
            exit();
        }

        if($sign=='-') {
            echo "Your answer is: " , $value1-$value2;
            exit();
        }

        if($sign=='*') {
            echo "Your answer is: " , $value1*$value2;
            exit();
        }

        if($sign=='/') {
            echo "Your answer is: " , $value1/$value2;
            exit();
        }
    }
?>

All I want to do is that answer should be displayed in the result input field instead of echoing them separately. Please help? I Know it's simple but I am new in PHP.

This question is related to php html variables

The answer is


One way to do it will be to move all the php code above the HTML, copy the result to a variable and then add the result in the <input> tag.
Try this -

<?php
//Adding the php to the top.
if(isset($_POST['submit']))
{
    $value1=$_POST['value1'];
    $value2=$_POST['value2'];
    $sign=$_POST['sign'];
    ...
        //Adding to $result variable
    if($sign=='-') {
      $result = $value1-$value2;
    }
    //Rest of your code...
}
?>
<html>
<!--Rest of your tags...-->
Result:<br><input type"text" name="result" value = "<?php echo (isset($result))?$result:'';?>">

inside the Form, You can use this code. Replace your variable name (i use $variable)

<input type="text" value="<?php echo (isset($variable))?$variable:'';?>">

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to variables

When to create variables (memory management) How to print a Groovy variable in Jenkins? What does ${} (dollar sign and curly braces) mean in a string in Javascript? How to access global variables How to initialize a variable of date type in java? How to define a variable in a Dockerfile? Why does foo = filter(...) return a <filter object>, not a list? How can I pass variable to ansible playbook in the command line? How do I use this JavaScript variable in HTML? Static vs class functions/variables in Swift classes?