Problem is from \"PHP Programming with MySQL\" Second Edition, Chapter, Exercise
ID: 3872509 • Letter: P
Question
Problem is from "PHP Programming with MySQL" Second Edition, Chapter, Exercise 4-5. The goal is two write a all-in-one sticky form that solves the "two trains are moving toward each other" word problem. My code validates but the form doesn't display. Any help would be appreciated. See code below.
<!--
Author: Jennifer Bailey
Class: CTP 130-400
Chapter 4: Handling User Input
Assignment: Lab 5 Exercsise 4-5
Date: 9/29/17
Doc Title: TwoTrains.php
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Lab 5: Handling User Input - Exercise 4-5</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="CTP 130 Lab 5 Assignment."/>
<meta name="keywords" content="CTP 130, Lab 5, PHP coding, book exercises"/>
<meta name="author" content="Jenn Bailey"/>
</head>
<body>
<?php
//delcare and initialize the variables
$DisplayForm = TRUE;
$errorCount = 0;
$SpeedA = " ";
$SpeedB = " ";
$Distance = " ";
if($DisplayForm == TRUE) {
if($errorCount > 0) {
echo "Please re-enter the form information below.<br/>";
displayForm($SpeedA, $SpeedB, $Distance);
} else
$DistanceA = round((($SpeedA/$SpeedB) * $Distance)/(1 + ($SpeedA/$SpeedB)),2);
$DistanceB = round($Distance - $DistanceA,2);
$TimeA = round($DistanceA/$SpeedA,2);
$TimeB = round($DistanceB/$SpeedB,2);
echo "Train A traveled $DistanceA miles in $TimeA hours at $SpeedA mph.<br/>";
echo "Train B traveled $DistanceB miles in $TimeB hours at $SpeedB mph.<br/>";
echo "The sum of the two distances is ".($DistanceA + $DistanceB)."miles.<br/>";
}
}
//check if data entered is numeric
if(isset($_POST['Submit'])) {
$SpeedA = validateInput($_POST['speedA'], "Speed of Train A");
$SpeedB = validateInput($_POST['speedB'], "Speed of Train B");
$Distance = validateInput($_POST['distance'], "Distance between trains");
//check error count
if($errorCount == 0) {
$DisplayForm = FALSE;
} else {
$DisplayForm = TRUE;
}
//display the form
function displayForm($SpeedA, $SpeedB, $Distance) {
?>
<h2>Two Trains</h2>
<form name="TwoTrains" action="TwoTrains.php" method="post">
<p>Speed of Train A: <input type="text" name="speedA" value=" " />mph</p>
<p>Speed of Train B: <input type="text" name="speedB" value=" " />mph</p>
<p>Distance between trains: <input type="text" name="distance" value=" " />miles</p>
<p><input type="reset" value="Clear Form" />
<input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}
//validate input
function validateInput($data, $fieldName) {
global $errorCount;
//check length of entered data
if(empty($data)) {
echo ""$fildName" is required.<br/>";
++$errorCount;
$retval = " ";
} else {
$retval = trim($data);
$retval = $stipslashes($retval);
//check if input is numeric
if(is_numeric($retval)){
//check value of input
if($retval <= 0) {
echo ""$fieldName" must be a number greater than 0.<br/>";
++$errorCount;
}
} else {//if input is not numeric
echo ""$fieldName" must be a number.<br/>";
++$errorCount;
$retval = " ";
}
}
return($retval);
}
?>
</body>
</html>
Explanation / Answer
Please Find the solution of your problems .
you miss the some braces so and your if condition not match the criteria.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Lab 5: Handling User Input - Exercise 4-5</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="CTP 130 Lab 5 Assignment."/>
<meta name="keywords" content="CTP 130, Lab 5, PHP coding, book exercises"/>
<meta name="author" content="Jenn Bailey"/>
</head>
<body>
<?php
//delcare and initialize the variables
$DisplayForm = TRUE;
$errorCount = 0;
$SpeedA = " ";
$SpeedB = " ";
$Distance = " ";
if($DisplayForm == TRUE) {
if($errorCount <= 0) {
echo "Please re-enter the form information below.<br/>";
displayForm($SpeedA, $SpeedB, $Distance);
} else{
$DistanceA = round((($SpeedA/$SpeedB) * $Distance)/(1 + ($SpeedA/$SpeedB)),2);
$DistanceB = round($Distance - $DistanceA,2);
$TimeA = round($DistanceA/$SpeedA,2);
$TimeB = round($DistanceB/$SpeedB,2);
echo "Train A traveled $DistanceA miles in $TimeA hours at $SpeedA mph.<br/>";
echo "Train B traveled $DistanceB miles in $TimeB hours at $SpeedB mph.<br/>";
echo "The sum of the two distances is ".($DistanceA + $DistanceB)."miles.<br/>";
}
}
//check if data entered is numeric
if(isset($_POST['Submit'])) {
$SpeedA = validateInput($_POST['speedA'], "Speed of Train A");
$SpeedB = validateInput($_POST['speedB'], "Speed of Train B");
$Distance = validateInput($_POST['distance'], "Distance between trains");
//check error count
if($errorCount == 0) {
$DisplayForm = FALSE;
} else {
$DisplayForm = TRUE;
}
}
//display the form
function displayForm($SpeedA, $SpeedB, $Distance) {
?>
<h2>Two Trains</h2>
<form name="TwoTrains" action="TwoTrains.php" method="post">
<p>Speed of Train A: <input type="text" name="speedA" value=" " />mph</p>
<p>Speed of Train B: <input type="text" name="speedB" value=" " />mph</p>
<p>Distance between trains: <input type="text" name="distance" value=" " />miles</p>
<p><input type="reset" value="Clear Form" />
<input type="submit" name="Submit" value="Send Form" /></p>
</form>
<?php
}
//validate input
function validateInput($data, $fieldName) {
global $errorCount;
//check length of entered data
if(empty($data)) {
echo ""$fildName" is required.<br/>";
++$errorCount;
$retval = " ";
} else {
$retval = trim($data);
$retval = $stipslashes($retval);
//check if input is numeric
if(is_numeric($retval)){
//check value of input
if($retval <= 0) {
echo ""$fieldName" must be a number greater than 0.<br/>";
++$errorCount;
}
} else {//if input is not numeric
echo ""$fieldName" must be a number.<br/>";
++$errorCount;
$retval = " ";
}
}
return($retval);
}
?>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.