7

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.

1
  • Move the PHP code to be above the HTML form. Assign the answer to a variable, then echo the variable into the input value. Commented Mar 18, 2014 at 15:35

3 Answers 3

19

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

2 Comments

@sam Did you remove the exit() functions?
@sam Sure you can, Add the php code(the part above the <html>) in another file(call it calc_logic.php), and before <html> just include it using <?php require("calc_logic.php"); ?>
3

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

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

Comments

1

Try this

<input  class="qtytext-box" type="number"  value= <?php echo  $colll2; ?> >

2 Comments

Please add some explanation to your answer.
I am using php echo for print value in input box

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.