0
Hello, i'm new in PHP so i dont know much about it so ill need a little help with this "eazy" problem. I need to calculate the total price of the items on selected radio button and checkboxes . here is my code:

<!doctype html> <html> <head> <meta charset="utf-8"> <title>Show selected items</title> </head> <body> <?php

if ($_POST) { // Store the form data in variables and trim unwanted spaces foreach ($_POST as $key => $value) {
${$key} = $value; }
//Initialise the error message to an empty string
$errors = "";

//Check if the first name has been entered
if ($first_name == ""){
    $errors .= "Please enter your first name.<br>";
    }

//Check if the last name has been entered
if ($last_name == ""){
    $errors .= "Please enter your last name.<br>";

}

//Check if the phone number has been entered as a number
if (is_numeric($phone_number) == FALSE){
    $errors .= "Please enter a valid phone number (numbers only).<br>";
}

//Inform user about any missing details
if ($errors != "") {
    echo "<p>Some of the required information has not been supplied.<br>";
    echo $errors;
    echo "Please click the browser's back button to try again.</p>";
}

else {
// Display the form data
echo <<<END <p>First name: $first_name<br> <br>Last name: $last_name<br> <br>Phone Number: $phone_number<br> END; } }
//Check if any radio buttons were selected
if (isset($_POST['mtype'])) { 
echo "<p>Membership type: ".$_POST['mtype']; //Display selected value
}

//Check if any checkboxes were selected 
if (isset($_POST['extras'])) {
    echo "<p>You selected the following extras.</p>";

//Get the array
$extras_array = $_POST['extras'];

//Display the selected checkbox as a list
echo "<ul>";
foreach ($extras_array as $extras){
    echo "<li>$extras";
}
echo "</ul>";
} else { echo "<p>You selected no extra services.</p>"; }
//Calculating cost

?> </body> </html>

Base fee for radio buttons are: Adult:$250, Child:$150, Concession:$150
I havent made my mind up what prices the checkboxes will be but it would be a great help if you can just tell me how to add the cost (of the selected checkboxes) up to the radio button. E.g 

If user chose Adult ($250) then Checkbox1 Checkbox2 Checkbox3 = 250 + * + * + * = ?
2
Contributors
1
Reply
24
Views
6 Months
Discussion Span
Last Post by diafol
0

Changing post keys into variables isn't always a great idea as you may get collisions with other existing variables - same as using extract() - so maybe avoid.

If a key has not been passed - you make no allowances for it with an isset() - you blindly assume it has been passed and check its value - this can lead to errors.

I think sorting out these issues first will make life easier.

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.