Question
I need to create an all-in-one sticky form with two functions: one to validate the input as numeric and greater than zero and one to
I need to create an all-in-one sticky form with two functions: one to validate the input as numeric and greater than zero and one to display the form. Plus extra code to execute the functions. While my code validates concerning syntax, I can't get the form to even display (unless I create a form in HTML first). This a problem from "PHP Programming with MySQL" Second Edition, Chapter 4, Exercise 4-5 if you need more details. I've reread the text, but I can't figure out what I'm doing wrong. My code is pasted below, thanks in advance for any suggestions.
Here is a link to the actual problem: https://www.chegg.com/homework-help/php-programming-with-mysql-2nd-edition-chapter-4-problem-5re-solution-9780538745840
Here is a link to how the program should run (this is not my work): http://j-journey.com/Chapter.04/Chapter_Homework/Ch%204_teacher%20Solution/TwoTrains.php
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Lab 5: Handling User Input - Exercise 4-5
//delcare and initialize the variables $DisplayForm = TRUE; $errorCount = 0; $SpeedA = " "; $SpeedB = " "; $Distance = " "; //if submit is clicked if(isset($_POST['Submit'])) { //check input $SpeedA = validateInput($_POST['speedA'], "Speed of Train A"); $SpeedB = validateInput($_POST['speedB'], "Speed of Train B"); $Distance = validateInput($_POST['distance'], "Distance between trains"); if($errorCount === 0) { $DisplayForm = FALSE; //calcuate distances and time $DistanceA = round((($SpeedA/$SpeedB) * $Distance)/(1 + ($SpeedA/$SpeedB)),2); $DistanceB = round(($Distance - $DistanceA),2); $TimeA = round($DistanceA/$SpeedA,2); $TimeB = round($DistanceB/$SpeedB,2); //display output echo "Train A traveled ".$DistanceA."miles in ".$TimeA."hours at ".$SpeedA."mph. "; echo "Train B traveled ".$DistanceB."miles in ".$TimeB."hours at ".$SpeedB."mph. "; echo "The sum of the two distances is ".($DistanceA + $DistanceB)."miles. "; } else { echo "Please re-enter the form information below. "; displayForm($SpeedA, $SpeedB, $Distance); } } //validate input function validateInput($data, $fieldName) { global $errorCount; if(empty($data)) { echo "\"$fieldName\" is a required field. "; ++$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. "; ++$errorCount; } } else {//if input is not numeric echo "\"$fieldName\" must be a number. "; ++$errorCount; $retval = " "; } } return($retval); } //display the form function displayForm($SpeedA, $SpeedB, $Distance) { ?>
Two Trains
Speed of Train A: mph
Speed of Train B: mph
Distance between trains: miles
} ?>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started