scifiguy Posted October 21, 2013 Share Posted October 21, 2013 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: <!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!!!!!!!!!!!!!!!!! Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 21, 2013 Share Posted October 21, 2013 (edited) WAre you unsure how to call the Calc function when someone enters values into the addition fields? How I'd do it is change the addition fields to <strong>Addition:</strong><br /> <input type="text" name="a1" value="<?php echo field('a1'); /* get field a1 value */ ?>" /> plus <input type="text" name="a2" value="<?php echo field('a2'); /* get field a2 value */ ?>" /> equals <input type="text" name="a3" value="<?php echo Calc(); /* print sum */ ?>" /> Now change the Calc function to function Calc() { $num1 = field('a1'); // get the value in a1 field $num2 = field('a2'); // get the value in a2 field if (!empty( $num1) && !empty( $num2 )) // make sure values are not empty { return $num1 + $num2; // return the sum } } // helper function to get values from $_POST function field($fieldname) { return isset($_POST[$fieldname]) ? $_POST[$fieldname] : ''; // return fields value } Edited October 21, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
scifiguy Posted October 21, 2013 Author Share Posted October 21, 2013 Thank you for your help with the addition method. I tried to duplicate what you did for the other methods, but all that outputs is the sum for Addition. I tried to change the if statements to else if structure, but it doesn't compile it just says unexpected else. Any ideas? I also need to finish changing the text based on operation. Thanks again. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 21, 2013 Share Posted October 21, 2013 (edited) the code I gave was an example for just the doing the addition operation. You'll need to write new functions for doing subtraction, multiplication and division operations. You don't modify the Calc function. To write the other three functions, copy and paste Calc function. Rename it to the operation you want to do, for example Sub for subtraction. Change field('a1') and field('a2') to so it gets the field values for the operation. So for subtraction that would be field('s1') and field('s2'). You'd change the sum to the operation, for subtraction change return $num1 + $num2; to return $num1 - $num2; When you want to perform the operation you'd call that function, for example the answer field for subtraction would be <input type="text" name="s3" value="<?php echo Sub(); /* print sum */ ?>" /> Edited October 21, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
scifiguy Posted October 21, 2013 Author Share Posted October 21, 2013 How can I use GET to echo out text? $_Get[status]; $status=Waiting for user input. echo $status; Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 21, 2013 Share Posted October 21, 2013 How can I use GET to echo out text? $_Get[status]; $status=Waiting for user input. echo $status; What? Can you rewrite your question I didn't understand that Quote Link to comment Share on other sites More sharing options...
scifiguy Posted October 21, 2013 Author Share Posted October 21, 2013 I need to have a statement when the page loads that says waiting for input. My instinct was to use the GET method. If so, how do I do that? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 21, 2013 Share Posted October 21, 2013 You only need to use get if you're passing data to different pages. Are you only wanting to display the waiting for input message when the user has not entered anything into the form. You can do if(empty($_POST)) { echo '<p>Waiting for input.</p>'; } Quote Link to comment Share on other sites More sharing options...
scifiguy Posted October 21, 2013 Author Share Posted October 21, 2013 It says unexpected {. I copied the function into a new php section. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 21, 2013 Share Posted October 21, 2013 There is nothing wrong with the if statement I posted. You have added it to your code incorrectly. Where did you add it? Quote Link to comment Share on other sites More sharing options...
scifiguy Posted October 21, 2013 Author Share Posted October 21, 2013 Nervermind. I got it to work. How can I use this to change the text depending on weather the number are added, subtracted, multipled, or divided? Quote Link to comment Share on other sites More sharing options...
scifiguy Posted October 22, 2013 Author Share Posted October 22, 2013 Does anybody know how to see if an inputted value on a form matches a variable? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 22, 2013 Share Posted October 22, 2013 You use the comparison operator == (two equal signs) if($variable == 'value') { // value matches } Quote Link to comment Share on other sites More sharing options...
scifiguy Posted October 22, 2013 Author Share Posted October 22, 2013 (edited) The goal of this part is to test if the user is right if they enter something in the results box. If the user is right, The program will output you are right, otherwise, It will output you are wrong. This is how I thought to do it, but it doesn't work: if ($_POST['a3']==$a3){ echo"You are right."; } { else echo"You are wrong."; } Edited October 22, 2013 by scifiguy Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 22, 2013 Share Posted October 22, 2013 (edited) if ($_POST['a3']==$a3){ should be if (field('a3') == Calc()){ Remember The field() helper function I wrote gets the value of the field that is pass to it, for example field('a3') gets the value from the field named a3 Calc() adds a1 and a2 input fields and returns the sum. if you are not using the field function then the if will be if ($_POST['a3'] == Calc()){ Edited October 22, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
scifiguy Posted October 22, 2013 Author Share Posted October 22, 2013 Nothing happens. It auto corrects to the right answer. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 22, 2013 Share Posted October 22, 2013 (edited) That is because you have this for the answer field for addition operation equals <input type="text" name="a3" value="<?php echo Calc(); /* print sum */ ?>" /> When the form is submitted the Calc function is ran. And so it fills in the answer field. If you only want it run if nothing has been entered into that field then change the Calc function to function Calc() { // if nothing is entered in the answer field // then calcute the awnser if(empty(field('a3'))) { $num1 = field('a1'); // get the value in a1 field $num2 = field('a2'); // get the value in a2 field if (!empty( $num1) && !empty( $num2 )) // make sure values are not empty { return $num1 + $num2; // return the sum } } // something is entered in the field else { // return what was entered return field('a3'); } } Edited October 22, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
scifiguy Posted October 22, 2013 Author Share Posted October 22, 2013 I got this error when I changed the function: Fatal error: Can't use function return value in write context in /var/www/index.php on line 42 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 22, 2013 Share Posted October 22, 2013 Change if(empty(field('a3'))) to if(field('a3') == '') Quote Link to comment Share on other sites More sharing options...
scifiguy Posted October 22, 2013 Author Share Posted October 22, 2013 You are right or wrong still did not echo. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 22, 2013 Share Posted October 22, 2013 Post your full code Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.