0

I am trying to learn this but I am so not logical and I'm lost as all hell.

Anyway I'll just copy paste what I've already put on yahoo answers.

Can you help me figure this out this php?
So far this is what I have and I'm lost on what else to do. I have a test over it tomorrow so I really just want to kind of get the homework done and study my homework for the test. I'm not catching this stuff so well, I have one more assignment after this that I'm sure I'll be asking for help on. Anyway, This is my assignment---
Create a form that asks the user for the length and width of a rectangle. When the user presses "Submit" (or whatever you call it) the page should call a php page in the hmwk10 folder that will calculate the following:
* The perimeter of the rectangle (two times the length plus width)
* The area of the rectangle (length times the width)
* The length of the diagonal (square root of length times length plus width times width)

This is what I have in my php file:

<?php

if($_POST['form_submit'] == 'TRUE')
{

$width = 0;
$length = 0;
$perimeter = 0;
$area = 0;
$diag = 0;

if(is_numeric($_POST['width']))
{
$width = $_POST['width'];
}

if(is_numeric($_POST['length']))
{
$length = $_POST['length'];

echo $output;

?>

This is what I have in my html file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml…

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP Sample</title>
</head>
<body>

<form action="process.php" method="post">
<div>
<input name="width" type="text" value="0"/>
<input name="length" type="text" value="0"/>
<input name="form_submit" type="hidden" value="TRUE"/>
<input type="submit" value="Calculate"/>
</div>
</form>

<hr />
<p><a href="http://validator.w3.org/check/refe… this page</a></p>
</body>
</html>

I'm confused. Someone helped with what I have so far and I'm still lost. Why am I putting zero after the width and lenght variables?

Also for the perimeter variable, shouldn't I have it say
$perimeter = (2 * $length) + $width;
and area
$area = $length * $width;
and for length of diaganoal
not sure how to do the length of the diagonal (square root of length times length plus width times width)

I really am lost on the logics of it all like what goes in php, what goes in html..bleh

Edited by Ezzaral: Added code tags. Please use them to format all code that you post.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by jcanaway
0

you need to better explain what you want to do for us to help you with what you need

0

This is easy:

For the PHP script:

<html>
<head>
  <title>PHP Sample</title>
</head>
<body>
<?php
$height = $_POST["height"];
$width = $_POST["width"];
echo "Parimeter: " . 2*($height+$width) . "<br />
Area: " . $height*$width . "<br />
Diagonal: " . sqrt(($height*$height)+($width*$width));
?>
</body>
</html>

For the form:

<html>
<head>
  <title>PHP Sample</title>
</head>
<body>
  <form method="post">
  <table><!-- I just like to use tables to arrange content -->
    <tr>
      <td>Height: </td>
      <td><input name="height" type="text" /></td>
    </tr>
    <tr>
      <td>Width: </td>
      <td><input name="width" type="text" /></td>
    </tr>
    <tr>
      <input name="submit" type="submit" value="Calculate" />
    </tr>
  </table>
  </form>
</body>
</html>

Edited by rbrt13: sorry, hit * instead of +

0

Here is a file you can use for your project Learn form it there are a whole bunch of comments in this to tell you what I've done i used a little bit of code from the other Reply but improved with in one file that is in the attachment so grab it if ya want if you liked it and i helped you give me some Positive post points thanks and your welcome

Attachment Here ->
rect.php

Attachments
<?php
error_reporting(E_ERROR); // this will not show the error on the document
// you must have variables defined otherwise you have errors
$err = ''; // defined variable
$width = $_POST['width']; // defined variable
$length = $_POST['length']; // defined variable
$rect = $_POST['rect']; // defined variable

if ($rect == true) { // this if statement is required to tell if the form has been submitted
	if (($width == '') || ($length == '')) { // this if statement is to check if width or length is = to blank or not if blank send back errors
		$err = 'Error in Calculating Rectangle<br/>';
		if (($width == '')) { //  this is to check if the width is = to blank
			$err .= 'Please insert width.<br/>';
		}
		if (($length == '')) { //  this is to check if the length is = to blank
			$err .= 'Please insert length.<br/>';
		}
	}else { // this else statement is if the width and length is both not blank then start calculating the form
		$err = "Parimeter: " . 2*($length+$width) . "<br />Area: " . $length*$width . "<br />Diagonal: " . sqrt(($length*$length)+($width*$width))."<br/>";
	}
}
?>
<html>
<head>
<title>Rectangle</title>
</head>

<body>
<?php
if ($err != '') { // this check if the error variable is not blank if so then print them here
	print($err.'<br/>');
}
?>
<form method="post">
	<input name="rect" type="hidden" value="true"/>
	<label>Length: <input name="length" type="text" value="<?php print($length); // if the length is not blank then reput the value in the field ?>" /></label><br/>
	<label>Width: &nbsp;<input name="width" type="text" value="<?php print($width); // if the length is not blank then reput the value in the field ?>" /></label>
	<input type="submit" value="Calculate"/>
</form>

</body>
</html>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.