Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation PHP Calculator issue

    Hello fellow coders I am having an issue coding a php calculator. My objective is to make it so that text changes depending on the operation chosen, and the answer is in the text box. if you dont input an answer, it will give you the right answer. here is how the website is supposed to act:http://usefullittlethings.com/static/index_done.php

    here is my code:
    Code:
    <!DOCTYPE html>
    <html>
    	<head>
    		<title>Online Math Calculator</title>
    	</head>
    	<body>
    		<h1>Thank you for trying out the online math calculator.</h1>
    		<h2>Enter two numbers to add, subtract, multiply, or divide.</h2>
    		<h3>If you enter the result, I will tell you if what you entered is right.</h3>
    		If you don't enter both numbers for a formula, I will ignore it.<br /><br />
    <?php
    $status= 'Waiting for input.<br>';
    echo $status;
    ?>
    
    <?php
    function Calc(){
    if (isset($a1)&&!isset($a2){
    if (!empty($a1)&&!empty($a2)
    $a3=$a1+$a2;
    echo $status.'two numbers added.'
    }
    }
    
    ?>
    
    		<form action="index.php" method="post">
    			<strong>Addition:</strong><br />
    			<input type="text" name="a1" value="" />
    			plus
    			<input type="text" name="a2" value="" />
    			equals
    			<input type="text" name="a3" value="" />
    			
    			<br /><br />
    			<strong>Subtraction:</strong><br />
    			<input type="text" name="s1" value="" />
    			minus
    			<input type="text" name="s2" value="" />
    			equals
    			<input type="text" name="s3" value="" />
    			
    			<br /><br />
    			<strong>Multiplication:</strong><br />
    			<input type="text" name="m1" value="" />
    			times
    			<input type="text" name="m2" value="" />
    			equals
    			<input type="text" name="m3" value="" />
    			
    			<br /><br />
    			<strong>Division:</strong><br />
    			<input type="text" name="d1" value="" />
    			divided by
    			<input type="text" name="d2" value="" />
    			equals
    			<input type="text" name="d3" value="" />
    			
    			<br /><br />
    			<input type="submit" value="Calculate!" />
    		</form>
    	</body>
    </html>
    Thanks. Any help is appreciated!!!!!!!!!!!!!!!!!

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 594 Times in 593 Posts
    To begin with your function is missing closing paraphrases:
    PHP Code:
    function Calc(){
        if (isset(
    $a1)&&!isset($a2)){         // THE FINAL ) MISSING
            
    if(!empty($a1) && !empty($a2))   // THE FINAL ) MISSING
                
    $a3 $a1 $a2;
            echo 
    $status 'two numbers added.';
        }

    You are not passing anything into the function.

    You are not getting the POST variables from the submitted form.

    You are not placing the answers(if you got them) into the equals <input>.

    Your calc function is set up for addition but not the other three your HTML shows.

    Does this help?
    PHP Code:
    <?php
    $status
    'Waiting for input.<br>';
    echo 
    $status;

    function 
    Calc($a1$a2){
        
    $a3 $a1 $a2;
        return 
    $a3;
    }
    echo 
    Calc(44);
    ?>
    Although I think I would delete the first two lines cause they do nothing for your program at all.
    Last edited by sunfighter; 10-21-2013 at 08:53 PM.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •