PauliePaulie Posted December 20, 2011 Share Posted December 20, 2011 Hello Everyone, I'm a newbie trying to improve my script below. The script works fine but I would like to add the 3 radio buttons computations enhancements Ergo, if a user chooses 'Matte Paper" a 10% would be added to the Price Quote result. If a user chooses ' Gloss Paper, a 10% would be added to the Price Quote result. If a user chooses 'Canvas" , a 20 % would be added to the Price Quote result. If someone could assist with the math or post me a helpful link it would be greatly appreciated. Thank you. Paul from Melbourne, Australia. <div style="border:dashed 1px; width:570px;"> <form method="post" action="<?php echo $_SERVER['php_SELF'];?>"> <p> 1. Enter the size (100w.x180h. cm) you want your picture printed: Width <input type="text" name="width" size="1" maxlength="3" value="0" onFocus="this.value=''; this.style.color='#ff0000';"> Height <input type="text" name="height" size="1" value="0" maxlength="3" value="0" onFocus="this.value=''; this.style.color='#ff0000';"> </p> <p> 2. Choose the printing material for your picture: <input type="radio" value="cms in Matte paper" name="material" size="3" maxlength="3" /> Matte Paper <input type="radio" value="cms in Gloss Paper:" name="material" size="3" maxlength="3" /> Gloss Paper <input type="radio" value="cms in Canvas:" name="material" size="3" maxlength="3" /> Canvas </p> <p> 3. Submit your info for our Price Quote below : <input type="submit" name="submit" value="Submit" /> or: <input type="submit" name="clear" value="Reset" /> </p> <p> 5. Our Price Quote to print your artwork sized: <?php $width = $_POST['width']; /* if artwork wider than 101 cm no quote, exits*/ if ($width >=101) {echo "Error!"; exit ;} $height = $_POST['height']; /* if artwork higher than 181 cm no quote, script exits */ if ($height >=181) {echo "Error!"; exit ;} $measurement = $_POST['measurement']; $material = $_POST['material']; $result = $width * $height; echo " $width x $height $material is: "; echo "$".number_format($result,2). "" ; ?> </p> </form> </div> Quote Link to comment https://forums.phpfreaks.com/topic/253570-php-form-math-help/ Share on other sites More sharing options...
Psycho Posted December 20, 2011 Share Posted December 20, 2011 There is nothing in that code to calculate a price, so it's kind of hard to add 10% or 20% to a value that does not exist! Also, something seems off about that script. The script seems to be primarily for entering the specifications, but there is some code at the end for calculating the area of the piece. You should have separate scripts for the form and the processing logic. They can be in the same file, but they should be logically separated. Quote Link to comment https://forums.phpfreaks.com/topic/253570-php-form-math-help/#findComment-1299892 Share on other sites More sharing options...
possien Posted December 20, 2011 Share Posted December 20, 2011 Try this for your radio buttons: 2. Choose the printing material for your picture: <input type="radio" value="1.1" name="material" size="3" maxlength="3" /> Matte Paper <input type="radio" value="1.1" name="material" size="3" maxlength="3" /> Gloss Paper <input type="radio" value="1.2" name="material" size="3" maxlength="3" /> Canvas Then your $result would be: $result = $width * $height * $material; Quote Link to comment https://forums.phpfreaks.com/topic/253570-php-form-math-help/#findComment-1299899 Share on other sites More sharing options...
Maq Posted December 20, 2011 Share Posted December 20, 2011 PauliePaulie, in the future, please place OR tags around your code. Quote Link to comment https://forums.phpfreaks.com/topic/253570-php-form-math-help/#findComment-1299908 Share on other sites More sharing options...
Psycho Posted December 20, 2011 Share Posted December 20, 2011 Try this for your radio buttons: 2. Choose the printing material for your picture: <input type="radio" value="1.1" name="material" size="3" maxlength="3" /> Matte Paper <input type="radio" value="1.1" name="material" size="3" maxlength="3" /> Gloss Paper <input type="radio" value="1.2" name="material" size="3" maxlength="3" /> Canvas Then your $result would be: $result = $width * $height * $material; But, then they would not have the information of what stock (i.e. paper) was selected for the estimate. I did consider the OP wanted to adjust the $result variable, but he stated in the description that he wanted to adjust the price. For all I know there are other determination value for the price. If he only wanted to adjust the 'area' value he did a poor job of explaining it. But, if that's the case then you would want to create an array to use both for the creation of the radio group and for calculating the adjustment. By using an array (or even a database) you don't have to worry about the radio group options getting out of sync with the code that does the calculation. Here is a more thorough script <?php $materialsList = array( 'Matte Paper' => .1, 'Gloss Paper' => .1, 'Canvas' => .2 ); //Create radio group options for material $materialGroup = ''; foreach($materialsList as $label => $value) { $materialGroup .= "<input type='radio' value='{$label}' name='material' size='3' maxlength='3' /> {$label}\n"; } //Determine quote if form was posted if($_SERVER['REQUEST_METHOD'] == 'POST') { $width = isset($_POST['width']) ? floatval($_POST['width']) : 0; $height = isset($_POST['height']) ? floatval($_POST['height']) : 0; $material = isset($_POST['material']) ? $_POST['material'] : false; $measurement = isset($_POST['measurement']) ? $_POST['measurement'] : false; $errors = array(); /* if artwork wider than 101 cm no quote, exits*/ if ($width >=101 || $width <= 0) { $errors[] = "Width must be between 1cm and 100cm"; } /* if artwork higher than 181 cm no quote, script exits */ if ($height >=101 || $height <= 0) { $errors[] = "Height must be between 1cm and 181cm"; } if(!isset($materialsList[$material])) { $errors[] = "Invalid material type selected"; } if(count($errors)) { $response = "The following errors occured:\n"; $response .= "<ul>\n"; foreach($errors as $error) { $response .= "<li>$error</li>\n"; } $response .= "</ul>\n"; } else { $adjustment = $materialsList[$material]; $price = ($width * $height); $price = $price * (1 + $adjustment); //Make adjustment $price = '$' . number_format($price, 2); //Format for display $response = "5. Our Price Quote to print your artwork sized:<br>\n"; $response .= "{$width} x {$height} cms in {$material} is: {$price}"; } } ?> <div style="border:dashed 1px; width:570px;"> <form method="post" action="<?php echo $_SERVER['php_SELF'];?>"> <p> 1. Enter the size (100w.x180h. cm) you want your picture printed: Width <input type="text" name="width" size="1" maxlength="3" value="0" onFocus="this.value=''; this.style.color='#ff0000';"> Height <input type="text" name="height" size="1" value="0" maxlength="3" value="0" onFocus="this.value=''; this.style.color='#ff0000';"> </p> <p> 2. Choose the printing material for your picture: <?php echo $materialGroup; ?> </p> <p> 3. Submit your info for our Price Quote below : <input type="submit" name="submit" value="Submit" /> or: <input type="submit" name="clear" value="Reset" /> </p> <p> <?php echo $response; ?> </p> </form> </div> Quote Link to comment https://forums.phpfreaks.com/topic/253570-php-form-math-help/#findComment-1299915 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.