Jump to content

Submit Return Answer


Barry2014

Recommended Posts

<?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

Link to comment
https://forums.phpfreaks.com/topic/292628-submit-return-answer/
Share on other sites

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";

Archived

This topic is now archived and is closed to further replies.

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