Barry2014 Posted November 21, 2014 Share Posted November 21, 2014 <?php $xb = 4; //width $yb = 2; //length function rectangle($x, $y) { $a = $x * $y; return $a; } ?> <br/> Please enter the values of the length and width of your rectangle. <br/><br/> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Width: <input type="text" name="" id="" value="2"> <br/><br/> Length: <input type="text" name="" id="" value="4"> <br/><br/> <input type="submit" name="submit" value="submit"> </form> <?php echo $rectangle; ?> I need to output the answer 8 when a user click on submit Quote Link to comment https://forums.phpfreaks.com/topic/292628-submit-return-answer/ Share on other sites More sharing options...
Ch0cu3r Posted November 21, 2014 Share Posted November 21, 2014 You need to call your rectangle function and pass it the $xb and $yb variables as arguments. To capture the return value you assign it to a variable when it is called. // pass $xb and $yb as arguments // assign return value to $result $result = rectangle($xb, $yb); // echo the result echo "$xb * $yb = $result"; If you are wanting to allow the user type the x and y values into the form. Then you will need to give a name to your two fields. Example Width: <input type="text" name="x" value="2"> Length: <input type="text" name="y" value="4"> You can then use $_POST['x'] and $_POST['y'] to get the submitted values and then you the pass those values to your function $xb = 2; $xb = 4; // override the default values of $xb and $yb with the values submitted by the form if($_SERVER['REQUEST_METHOD'] == 'POST) { $xb = $_POST['x']; $xy = $_POST['y']; } // pass values to the function. Capture the return value $result = rectangle($xb, $yb); // output the result echo "$xb * $yb = $result"; Quote Link to comment https://forums.phpfreaks.com/topic/292628-submit-return-answer/#findComment-1497258 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.