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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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