GetReady Posted July 30, 2010 Share Posted July 30, 2010 This wont work for some reason and its begining to get to me, any chance of some help? <?php /* Calculator */ if($submit) { echo $numa / $numb / $numb2; { } else { ?> <form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>"> <p>number 1: </p> <p> <input type="text" name="numa" size="10"> </p> <p>number 2: </p> <p> <input type="text" name="numb" size="10"> </p> <p>number 3: </p> <p> <input type="text" name="numb2" size="10"> </p> <p> <input type="submit" value="Calculate output!" name="submit"> </p> </form> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/209353-any-clue-as-to-where-i-went-wrong/ Share on other sites More sharing options...
Pikachu2000 Posted July 30, 2010 Share Posted July 30, 2010 Initially, you have an extra closing curly brace, but what is this code supposed to do? Echo the result of $numa divided by $numb divided by $num2, or just echo their values separated by slashes? EDIT: It also appears to rely on register_globals = On, which it shouldn't be, so you need to assign integer values to the variables via $numa = (int) $_POST['numa'];, etc. if($submit) { echo $numa / $numb / $numb2; { // get rid of this one. } else { ?> Link to comment https://forums.phpfreaks.com/topic/209353-any-clue-as-to-where-i-went-wrong/#findComment-1093161 Share on other sites More sharing options...
GetReady Posted July 30, 2010 Author Share Posted July 30, 2010 Sorry my php skills are amateur to say the least, Its supposed to divide each number e.g numa divided by numb divided by numb2, Its how ever not echoing the results... I take it ive gone wrong with that function somehow also? Any help is appreciated Thanks! Link to comment https://forums.phpfreaks.com/topic/209353-any-clue-as-to-where-i-went-wrong/#findComment-1093169 Share on other sites More sharing options...
Pikachu2000 Posted July 30, 2010 Share Posted July 30, 2010 See my edit above regarding register_globals. None of the variables will have values unless a) register_globals = On (wrong way) - OR - b) they are assigned values based on the values in the $_POST array (right way). Link to comment https://forums.phpfreaks.com/topic/209353-any-clue-as-to-where-i-went-wrong/#findComment-1093178 Share on other sites More sharing options...
aleX_hill Posted July 30, 2010 Share Posted July 30, 2010 in that case you will also need to use if($_POST['submit']) Link to comment https://forums.phpfreaks.com/topic/209353-any-clue-as-to-where-i-went-wrong/#findComment-1093180 Share on other sites More sharing options...
TOA Posted July 30, 2010 Share Posted July 30, 2010 Try something like this <?php // This says if submit has been hit process it otherwise show the form if (isset($_POST['submit'])) { // process // Get all the values the form sent from $_POST $numa = $_POST['numa']; $numb = $_POST['numb']; $num2 = $_POST['num2']; // Do the division $result = $numa/$numb/$num2; // echo the result echo $result; } else { // display ?> <form method="POST"> <!-- No need to use PHP_SELF...if it's not specified, it will automatically use itself --> <p>Number 1:</p><p><input type="text" name="numa" size="10"></p> <p>Number 2:</p><p><input type="text" name="numb" size="10"></p> <p>Number 3:</p><p><input type="text" name="num2" size="10"></p> <p><input type="submit" name="submit" value="Calculate output!" /></p> </form> <?php } ?> Hope I understood you right. This allows you to verify all the variables hold what they should Link to comment https://forums.phpfreaks.com/topic/209353-any-clue-as-to-where-i-went-wrong/#findComment-1093188 Share on other sites More sharing options...
GetReady Posted July 30, 2010 Author Share Posted July 30, 2010 Thanks guys, Yea DevilsAdvocate that worked a charm. One thing thought does anyone know of a way in which that code could echo the results at the bottom of the page instead of going to a blank page.. Thanks. Link to comment https://forums.phpfreaks.com/topic/209353-any-clue-as-to-where-i-went-wrong/#findComment-1093228 Share on other sites More sharing options...
aleX_hill Posted July 31, 2010 Share Posted July 31, 2010 If you mean without re-loading the page, you will need some sort of client side AJAX script, I use jQuery. The other option is to submit to itself (like you are) and then put the echo command after the form (remove the if statement to check if it has been submitted). If you wanted the values to stay in the form, use value="<?php echo $_POST['numa']; ?>" to set the values of the field. Link to comment https://forums.phpfreaks.com/topic/209353-any-clue-as-to-where-i-went-wrong/#findComment-1093401 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.