Jump to content

php simple calculator help


shahg

Recommended Posts

I have made a simple calculator but i am unable to print the result value in the form.please help.please check my coding.

 

Html coding:

 

 

<html>
    <head>
        <title>calculator</title>
        <link href="new.css" type="text/css" rel="stylesheet"/>


        
    </head>

    <body>
        <form action="new.php" method="POST" name="calculator">
        
     <h1> calculator</h1>
    <p>
    <strong>Frsit value</strong>    
    <input name="f_value" type="text" value="<?php $first_value ; ?>"  maxlength="50">
    
    <p><strong>Second value</strong> <input name="s_value" type="text" value="<?php $sec_value; ?>" maxlength="50">
    <p>
     <input name="add" type="submit" value="add">
    <!-- <input name="sub" type="submit" value="sub">
     <input name="sub" type="submit" value="multiply">
     <input name="sub" type="submit" value="divide">-->
    <p>
      
    <h2 style="border:thick">Result
      <input name="result" type="text" maxlength="50" value="<?php echo $result ; ?>" >
</h2>
    
        
        </form>
    </body>

</html>
 

Css :

 

 

form {
    display:block;
    background-color: #333399;
    width:300px;
    height:500px;
    border:thick;
    border: #330000;
    
    color: #FFCC00;
    }
    
    
h1 {
    text-align:center;
    z-index: 2px;
    }
 

php coding :

 

 

<?php
    $first_value = $_POST['f_value'];
    $sec_value = $_POST['s_value'];
    $error = "";
    
    //--calculation variables---//
    
    
    if(isset($_POST['add'])){
        
         $result = $first_value + $sec_value;
         header("location: new.html");
        
        
    }
   

Please tell me how to get my value inside result field.

Link to comment
https://forums.phpfreaks.com/topic/277018-php-simple-calculator-help/
Share on other sites

 

<html>
    <head>
        <title>calculator</title>
        <link href="new.css" type="text/css" rel="stylesheet"/>
        
        <?php
    
if(isset($_POST['add'])){
    $first_value = $_POST['f_value'];
    $sec_value = $_POST['s_value'];
    
    //--calculation variables---//
    
         $result = $first_value + $sec_value;            
    
    
        }
        ?>    

    </head>
<body>
        <form  method="post" action="new.php" name="calculator">
        
     <h1> calculator</h1>
    <p>
    <strong>Frsit value</strong>    
    <input  type="text" name="f_value" >
    
    <p><strong>Second value</strong> <input type="text" name="s_value" maxlength="50">
    <p>
     <input name="add" type="submit" value="add" >
    <!-- <input name="sub" type="submit" value="sub">
     <input name="sub" type="submit" value="multiply">
     <input name="sub" type="submit" value="divide">-->
    <p>
      
    <h2 style="border:thick">Result
      <input type="text" maxlength="50" value="<?php echo $result ; ?>" Name='result' >
    </h2>
    
        
        </form>
        
</body>

</html>

I have edit it but getting error that variable result is not defined

  • 5 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.