In: Computer Science
Using PHP, Make a form that allows the user to enter the weight
of the item being shipped. This will be used to calculate the
shipping cost.Create a form that uses the method POST
The form should capture a customer's package weight with the one
field for the package
weight in pounds.
All fields should have the REQUIRED attribute.
The form should have a submit button and a reset button.
The form should look nice. All the labels should line up
horizontally and all the
INPUTS/SELECT should line up horizontally.
Below is the solution:
shippingCost.php:
<?php
//
if(isset($_POST['submit'])){ //Calculate button click event
$weight = $_POST['weight']; //take input from textbox
if($weight == ""){ //check for the empty weight input box
echo "<script type='text/javascript'>alert('Enter weight');</script>";
}else{
$shipping = 'Shipping cost is : $'. (.01 * $weight); //calculate the shipping cost by 0.01 per pound
}
}
else if(isset($_POST['reset'])){ //reset button click event
$shipping=""; //rest the shipping value
}
?>
<!doctype html>
<head>
<meta charset="utf-8">
<title>Shipping Cost Calculator</title>
</head>
<style>
/* CSS Document */
body{
font-family:Arial,Helvectica, san serif;
width:40%;
margin:0 auto;
}
</style>
<body>
<h1>Shipping Cost Calculator</h1>
<fieldset>
<legend>Weight cost:</legend>
<form action="shippingCost.php" method="post">
<table>
<tr>
<td><label>Enter weight of the item (in pound):</label></td>
<td><input type="text" name="weight" size="10" maxlength="10" /></td>
</tr>
<tr>
<td></td>
<td><br>
<input type="submit" value="Calculate" name="submit"/>
<input type="submit" value="Reset" name="reset" />
</td>
</tr>
<tr>
<td><br><br>
<?php echo $shipping; //print the shipping cost?>
</td>
</tr>
</table>
</form>
</fieldset>
</body>
</html>
sample output:
Enter 2000 pound and the cost is $20