Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using PHP, build a form that will allow a user to type in the price of a candy i

ID: 3711157 • Letter: U

Question

Using PHP, build a form that will allow a user to type in the price of a candy in addition the number of quarters, dimes, nickels and pennies they have. After the user submits their information, the form should go to a new page and tell them how much money they have, how many candies they can buy, and what their change will be. If they don't have enough money to buy candy, let them know. The form should check to make sure valid information was typed in. You can use is_numeric() or empty() if you want. You can also check to make sure the input isn't an empty string "" or less than 0 and let the user know if their info was wrong. The form's method should also be set to "get" so that you can see the output inside of the URL at the top of the browser window. When you tell the user their change, it should be broken up into quarters, dimes, nickels, and pennies. You'll need to have a variable to store the change, and other variables for the amount of coins left over.

Explanation / Answer

For the given problem we required fields like total money, the no of candies can buy.

It should validate whether the entered value is numeric or not. If validation fails it should display the message also.

Sample php code for the given problem given below

<form

action="/action_page.php">

Total money: <input type="text" name="money" value="<?php echo $ money;?>">

No of candies that can buy

<input type="text" name="noCandies" value="<?php echo $ noCandies;?>">

<input type="submit" value="Submit">

</form>

The validation code is given below

<?php

// define variables and set to empty values

$ moneyErr = $ moneyErr = $ moneyErr = $websiteErr = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")

{

if (empty($_POST["money"]))

{

$nameErr = "money is required";

}

else { $name = test_input($_POST["money"]);

}

if(preg_match('/^d+$/',$_GET['id']))

{

// valid input.

}

else

{

// invalid input.

}

}

?>

This validation code will check the fields is filled or not and it will also check whether the field is taking integer or not

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote