Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

PHP Functions Ex. #3: Arguments from User Input

For this PHP exercise, you will rewrite the rectangle area function from #2 once again, this time to accept user input. Present a form to the user with the message "Please enter the values of the length and width of your rectangle." Below this, supply two text boxes, one for length and one for width. Using your function to process the user supplied values, return the result statement from the previous exercise to the user. Reminder: the statement was "A rectangle of length $l and width $w has an area of $area.", where $l and $w are the arguments and $area is the result.

 

Comments

Hi,

I like your site very much. Thanks for the hard work.
May I know how to put some validation code in above example? Like that user needs to enter only numeric value.

If you find my code awkward or found some bugs please comment. I myself is still learning. :)

<form action="rectanglearea.php" method="post">
	Enter length: <input type="text" name="textL" />
    Enter width: <input type="text" name="textW" />
    <input type="submit" value="Submit" />
    
</form>

<?php
if(isset($_POST['textL']) && isset($_POST['textW'])){
	if(!empty($_POST['textL']) && !empty($_POST['textW'])){
		if(!preg_match("([0-9])",$_POST['textL']) || !preg_match("([0-9])",$_POST['textW'])){
			echo 'Enter only numbers.';
		}
		else{	
			$l = $_POST['textL'];
			$w = $_POST['textW'];
			echo "The rectangle with length $l and width $w has an area of ".calculate($l,$w);
		}
	}	
}

function calculate($length, $width){
$area = $length * $width;
return $area;
}
?>

hope this one helps.

Great site - thanks muchly !!!

It is so good to find a commonsense approach to teaching the basics.

Ref this exercise I found the following works and perhaps the layout is easier to grasp (but what do I know - I'm only learning). Thanks again, and keep them coming, it is appreciated.

'H'

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html</a> xmlns="<a href="http://www.w3.org/1999/xhtml">
  4.  
  5. <head>
  6. <meta">http://www.w3.org/1999/xhtml">
  7.  
  8. <head>
  9. <meta</a> content="text/html; charset=utf-8" http-equiv="Content-Type" />
  10. <title>Untitled 1</title>
  11. <style type="text/css">
  12.  
  13. body {
  14. 	margin-left:300px;
  15. }
  16.  
  17. </style>
  18.  
  19.  
  20. </head>
  21.  
  22. <body>
  23. 	<?php
  24.  
  25. 	date_default_timezone_set('Europe/London');
  26.  
  27. //rewrite the rectangle area function from #2 once again, this time to accept user input. 
  28. //Present a form to the user with the message "Please enter the values of the length and width of your rectangle." 
  29. //Below this, supply two text boxes, one for length and one for width. 
  30. //Using your function to process the user supplied values, return the result statement from the previous exercise to the user. 
  31. //Reminder: the statement was "A rectangle of length $l and width $w has an area of $area.", where $l and $w are the arguments and $area is the result.
  32.  
  33. 	if (!isset($_POST['submit']))
  34. 		{
  35. ?>
  36. 	<form action="input_function.php" method="post">
  37. 	<h2>Please enter the values of the length and width of your rectangle.</h2>
  38. 	<p>Length: </p><input type="text" name="length" /><br />
  39. 	<p>Width: </p><input type="text" name="width" /><br />
  40. 	<br />
  41. 	<input type="submit" name="submit" />
  42. 	</form>
  43. <?php
  44.  
  45. 	}else{		
  46.  
  47. 		function calc()
  48. 	{
  49. 		$w = $_POST['width'];
  50. 		$l = $_POST['length'];
  51. 		$a = $l * $w;	
  52.  
  53. 		echo "A rectangle of length $l and width $w has an area of $a.";
  54. 	}
  55. 	calc();
  56. 	}
  57.  
  58. ?>
  59. </body>
  60.  
  61. </html>

Thanks for the alternative answer and sorry it took so long to post your comment.

I assume that you know your code won't work as written because you have not included the form HTML for the user input.

But your function does work. However, you may find a need to use a function more than once and in different parts of your script. In that case, it's better to declare it at the top.

Also, the intent of the exercise was to use arguments in your function. Your function uses the raw data from the $_POST variable and takes no arguments. That makes it less flexible.