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.

Edited by ignace
Added code tags
Link to comment
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

Edited by ignace
Added code tags
Link to comment
Share on other sites

  • 5 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.