j05hr Posted June 22, 2010 Share Posted June 22, 2010 I have a form http://www.bulletproofwebdesign.co.uk/orderform/calculator.php which calculates the quantity of the business cards you have ordered. What I want to do is charge different prices for single sided or double sided cards. This is the function that does that calculation function calculate_total ($qty, $cost, $tax = 0) { $single = ($qty * 5); $double = ($qty * 10); $total = ($qty * 10); And this is the bit that returns the data return number_format($total, 2); How can I make the radio buttons select the or $double or $single? and then return them? Whole code <?php # Script 3.10 - calculator.php #5 $page_title = 'Digital Studio Cost Calculator'; include ('includes/header.html'); /* This function calculates a total and then returns the results. */ function calculate_total ($qty, $cost, $tax = 0) { $single = ($qty * 5); $double = ($qty * 10); $total = ($qty * 10); return number_format($total, 2); } // End of function. // Check for form submission: if (isset($_POST['submitted'])) { // Minimal form validation: if ( is_numeric($_POST['quantity']) ) { // Print the heading: $success = 'Upload front side of card: <input name="image" accept="image/jpeg" type="file"> '; // Call the function, with or without tax: if (is_numeric($_POST['tax'])) { $sum = calculate_total ($_POST['quantity'], $_POST['price'], $_POST['tax']); } else { $sum = calculate_total ($_POST['quantity'], $_POST['price']); } // Print the results: //echo '<p style="font-weight: bold; color: #C00">The total cost of purchasing ' . $_POST['quantity'] . ' business card(s), is £' . $sum . '.</p>'; } else { // Invalid submitted values. echo '<h1>Error!</h1> <p class="error">Please enter a valid quantity and price.</p>'; } } // End of main isset() IF. // Leave the PHP section and create the HTML form: ?> <h1>Digital Studio Order Form</h1> <form action="calculator.php" method="post"> <p>Quantity: <select name="quantity"> <option value= "100" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 100) echo 'selected="selected"'; ?>>100</option> <option value= "250" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 250) echo 'selected="selected"'; ?>>250</option> <option value= "500" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 500) echo 'selected="selected"'; ?>>500</option> </select> </p> <p>Double sided: <INPUT TYPE="radio" NAME="q1" VALUE="yes"> Single Sided <INPUT TYPE="radio" NAME="q1" VALUE="no"> <?php if (!empty($success)) { echo "<p class=\"messge\">" . $success . "</p>"; } ?> <p>Total: £<?php echo $sum?></p> <p><input type="submit" name="submit" value="Calculate!" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php // Include the footer: include ('includes/footer.html'); ?> Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted June 22, 2010 Share Posted June 22, 2010 How can I make the radio buttons select the or $double or $single? and then return them? Are you talking about retrieving the radio selection? It's the same just that you are looking for what the value of the radio button is selected as. I think you just confused yourself is all, let's change yes and no to DOUBLE & SINGLE for the value of the radios. Then you check your $_POST for q1 and check if the value is equal to DOUBLE or SINGLE. Double sided: <INPUT TYPE="radio" NAME="q1" VALUE="DOUBLE"> <br /> Single Sided: <INPUT TYPE="radio" NAME="q1" VALUE="SINGLE"> Quote Link to comment Share on other sites More sharing options...
j05hr Posted June 22, 2010 Author Share Posted June 22, 2010 Yeah retrieving the radio select. I changed the radio buttons so now I have <?php # Script 3.10 - calculator.php #5 $page_title = 'Digital Studio Cost Calculator'; include ('includes/header.html'); /* This function calculates a total and then returns the results. */ function calculate_total ($qty, $cost, $tax = 0) { $single = ($qty * 5); $double = ($qty * 10); $total = ($qty * 0.05); return number_format($total, 2); } // End of function. // Check for form submission: if (isset($_POST['submitted'])) { // Minimal form validation: if ( is_numeric($_POST['quantity']) ) { // Print the heading: $success = 'Upload front side of card: <input name="image" accept="image/jpeg" type="file"> '; // Call the function, with or without tax: if (is_numeric($_POST['tax'])) { $sum = calculate_total ($_POST['quantity'], $_POST['price'], $_POST['tax']); } else { $sum = calculate_total ($_POST['quantity'], $_POST['price']); } // Print the results: //echo '<p style="font-weight: bold; color: #C00">The total cost of purchasing ' . $_POST['quantity'] . ' business card(s), is £' . $sum . '.</p>'; } else { // Invalid submitted values. echo '<h1>Error!</h1> <p class="error">Please enter a valid quantity and price.</p>'; } } // End of main isset() IF. // Leave the PHP section and create the HTML form: ?> <h1>Digital Studio Order Form</h1> <form action="calculator.php" method="post"> <p>Quantity: <select name="quantity"> <option value= "100" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 100) echo 'selected="selected"'; ?>>100</option> <option value= "250" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 250) echo 'selected="selected"'; ?>>250</option> <option value= "500" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 500) echo 'selected="selected"'; ?>>500</option> </select> </p> Double sided: <INPUT TYPE="radio" NAME="q1" VALUE="DOUBLE"> <br /> Single Sided: <INPUT TYPE="radio" NAME="q1" VALUE="SINGLE"> <?php if (!empty($success)) { echo "<p class=\"messge\">" . $success . "</p>"; } ?> <p>Total: £<?php echo $sum?></p> <p><input type="submit" name="submit" value="Calculate!" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php // Include the footer: include ('includes/footer.html'); ?> Which $_POST am I checking for q1? this one? if (isset($_POST['submitted'])) { Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted June 22, 2010 Share Posted June 22, 2010 if (isset($_POST['q1'])) { Quote Link to comment Share on other sites More sharing options...
j05hr Posted June 22, 2010 Author Share Posted June 22, 2010 The single one now works but the double one doesn't. I made an if statement for it. <?php # Script 3.10 - calculator.php #5 $page_title = 'Digital Studio Cost Calculator'; include ('includes/header.html'); /* This function calculates a total and then returns the results. */ function calculate_total ($qty, $cost, $tax = 0) { $single = ($qty * 5); $double = ($qty * 10); $total = ($qty * 0.05); return number_format($total, 2); } // End of function. // Check for form submission: if (isset($_POST['q1'])) { } if (isset($_POST['q2'])) { // Minimal form validation: if ( is_numeric($_POST['quantity']) ) { // Print the heading: $success = 'Upload front side of card: <input name="image" accept="image/jpeg" type="file"> '; // Call the function, with or without tax: if (is_numeric($_POST['tax'])) { $sum = calculate_total ($_POST['quantity'], $_POST['price'], $_POST['tax']); } else { $sum = calculate_total ($_POST['quantity'], $_POST['price']); } // Print the results: //echo '<p style="font-weight: bold; color: #C00">The total cost of purchasing ' . $_POST['quantity'] . ' business card(s), is £' . $sum . '.</p>'; } else { // Invalid submitted values. echo '<h1>Error!</h1> <p class="error">Please enter a valid quantity and price.</p>'; } } // End of main isset() IF. // Leave the PHP section and create the HTML form: ?> <h1>Digital Studio Order Form</h1> <form action="calculator.php" method="post"> <p>Quantity: <select name="quantity"> <option value= "100" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 100) echo 'selected="selected"'; ?>>100</option> <option value= "250" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 250) echo 'selected="selected"'; ?>>250</option> <option value= "500" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 500) echo 'selected="selected"'; ?>>500</option> </select> </p> <p>Double sided: <INPUT TYPE="radio" NAME="q1" VALUE="double"> <br /> <br /> Single Sided: <INPUT TYPE="radio" NAME="q2" VALUE="single"> <?php if (!empty($success)) { echo "<p class=\"messge\">" . $success . "</p>"; } ?> <p>Total: £<?php echo $sum?></p> <p><input type="submit" name="submit" value="Calculate!" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php // Include the footer: include ('includes/footer.html'); ?> Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted June 22, 2010 Share Posted June 22, 2010 My bad sorry... I meant this: if (isset($_POST['q1'])) { $Q1_Value = $_POST['q1']; } //Dump the value of the radio into a variable //Then do an if statements for what the value of that variable is if($Q1_Value == "SINGLE") { //Do something if single sided } if($Q1_Value == "DOUBLE") { //Do something if doublesided } Technically the radio button named "q1" will always exist since there is always a selection so it's not a matter of if that value exists... use "q1" as the name for all the radios in a set... and change it via the value of that radio set that would be named "q1". So don't rename the double one just check the value of the radio set. I didn't explain that right to begin with sorry a bit distracted. I apologize for that. Quote Link to comment Share on other sites More sharing options...
j05hr Posted June 22, 2010 Author Share Posted June 22, 2010 That's ok, They are still coming out as the same price which I don't understand as they have different values <?php # Script 3.10 - calculator.php #5 $page_title = 'Digital Studio Cost Calculator'; include ('includes/header.html'); /* This function calculates a total and then returns the results. */ function calculate_total ($qty, $cost, $tax = 0) { $single = ($qty * 5); $double = ($qty * 10); $total = ($qty * 0.05); return number_format($total, 2); } // End of function. // Check for form submission: if (isset($_POST['q1'])) { $Q1_Value = $_POST['q1']; } //Dump the value of the radio into a variable //Then do an if statements for what the value of that variable is if($Q1_Value == "SINGLE") { //Do something if single sided } if($Q1_Value == "DOUBLE") { //Do something if doublesided } { // Minimal form validation: if ( is_numeric($_POST['quantity']) ) { // Print the heading: $success = 'Upload front side of card: <input name="image" accept="image/jpeg" type="file"> '; // Call the function, with or without tax: if (is_numeric($_POST['tax'])) { $sum = calculate_total ($_POST['quantity'], $_POST['price'], $_POST['tax']); } else { $sum = calculate_total ($_POST['quantity'], $_POST['price']); } // Print the results: //echo '<p style="font-weight: bold; color: #C00">The total cost of purchasing ' . $_POST['quantity'] . ' business card(s), is £' . $sum . '.</p>'; } else { // Invalid submitted values. echo '<h1>Error!</h1> <p class="error">Please enter a valid quantity and price.</p>'; } } // End of main isset() IF. // Leave the PHP section and create the HTML form: ?> <h1>Digital Studio Order Form</h1> <form action="calculator.php" method="post"> <p>Quantity: <select name="quantity"> <option value= "100" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 100) echo 'selected="selected"'; ?>>100</option> <option value= "250" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 250) echo 'selected="selected"'; ?>>250</option> <option value= "500" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 500) echo 'selected="selected"'; ?>>500</option> </select> </p> <p>Double sided: <INPUT TYPE="radio" NAME="q1" VALUE="DOUBLE"> <br /> <br /> Single Sided: <INPUT TYPE="radio" NAME="q2" VALUE="SINGLE"> <?php if (!empty($success)) { echo "<p class=\"messge\">" . $success . "</p>"; } ?> <p>Total: £<?php echo $sum?></p> <p><input type="submit" name="submit" value="Calculate!" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php // Include the footer: include ('includes/footer.html'); ?> Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted June 22, 2010 Share Posted June 22, 2010 Because you if you are using that code you're not doing anything to them to change it as far as I can see, let me run the code and see if I can adjust stuff and give you an actual working example. Quote Link to comment Share on other sites More sharing options...
j05hr Posted June 22, 2010 Author Share Posted June 22, 2010 Thanks a lot for all your help. Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted June 22, 2010 Share Posted June 22, 2010 Rewrote some of it, not sure what some of it was doing so changed it on the fly.... but let me know if that does what it's expected and play around with the calculations shouldn't break it. But right now it's charging 10cents per double sided and 5 cents per single sided Also added code so that when it reloads it puts the double or single checked via what it was from last POST not sure if that's what you wanted though, and it defaults to single auto checked. As far as tax is involved too, I made the without tax call to the function equal to one so you have to no special calls in the function to deal with tax, ie if there is no tax it multiplies it by 1 giving same number. <?php # Script 3.10 - calculator.php #5 $page_title = 'Digital Studio Cost Calculator'; /* This function calculates a total and then returns the results. */ function calculate_total ($qty, $cost, $tax = 0, $radio) { // if single sided times qty by .05 if($radio == "SINGLE"){$total = $qty * .05;} // not sure you calculations so adjust here $single = ($qty * 5); // if doubke sided times qrt by .10 if($radio == "DOUBLE"){$total = $qty * .10;}// read above $double = ($qty * 10); //$total = ($qty * 0.05); // not sure why you had the total here like this... $total = $total * $tax; return number_format($total, 2); } // End of function. // Check for form submission: if (isset($_POST['q1'])) { $Q1_Value = $_POST['q1']; } //Dump the value of the radio into a variable //Then do an if statements for what the value of that variable is { // Minimal form validation: if ( is_numeric($_POST['quantity']) ) { // Print the heading: $success = 'Upload front side of card: <input name="image" accept="image/jpeg" type="file"> '; // Call the function, with or without tax: if (is_numeric($_POST['tax'])) { $sum = calculate_total ($_POST['quantity'], $_POST['price'], $_POST['tax'],$Q1_Value); } else { $sum = calculate_total ($_POST['quantity'], $_POST['price'], $tax = 1,$Q1_Value); } // Print the results: //echo '<p style="font-weight: bold; color: #C00">The total cost of purchasing ' . $_POST['quantity'] . ' business card(s), is £' . $sum . '.</p>'; } else { // Invalid submitted values. echo '<h1>Error!</h1> <p class="error">Please enter a valid quantity and price.</p>'; } } // End of main isset() IF. // Leave the PHP section and create the HTML form: ?> <h1>Digital Studio Order Form</h1> <form action="calculator.php" method="post"> <p>Quantity: <select name="quantity"> <option value= "100" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 100) echo 'selected="selected"'; ?>>100</option> <option value= "250" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 250) echo 'selected="selected"'; ?>>250</option> <option value= "500" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 500) echo 'selected="selected"'; ?>>500</option> </select> </p> <p>Double sided: <INPUT TYPE="radio" NAME="q1" VALUE="DOUBLE" <?php if($Q1_Value == "DOUBLE"){ echo "CHECKED";} ?> > <br /> <br /> Single Sided: <INPUT TYPE="radio" NAME="q1" VALUE="SINGLE" <?php if($Q1_Value != "DOUBLE"){ echo "CHECKED";} ?> > <?php if (!empty($success)) { echo "<p class=\"messge\">" . $success . "</p>"; } ?> <p>Total: £<?php echo $sum?></p> <p><input type="submit" name="submit" value="Calculate!" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php // Include the footer: include ('includes/footer.html'); ?> Quote Link to comment Share on other sites More sharing options...
j05hr Posted June 22, 2010 Author Share Posted June 22, 2010 That's exactly what I wanted it to do, thanks so much. Is it possible to explain what you did as I'm trying to learn as well rather then get someone to do my work and not learn what's happening. Yeah they should be checked after you do the calculation, otherwise it's very confusing. The next bit is adding two upload files for the double sided (like the one for the single side) I'm sure I will be back here asking for help. Thanks again, It's really appreciated. Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted June 22, 2010 Share Posted June 22, 2010 Give me some time and I'll take the script I adapted and comment all the way through it. So you can follow the path of. May take me a few hours to get to it though so be patient please. But yeah I'll get an explantion to yah Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted June 22, 2010 Share Posted June 22, 2010 <?php # Script 3.10 - calculator.php #5 $page_title = 'Digital Studio Cost Calculator'; /* This function calculates a total and then returns the results. */ function calculate_total ($qty, $tax = 0, $radio) { // I get three things for my function the $qty , the $tax & Radio button value $radio // if single sided times qty by .05 if($radio == "SINGLE"){$total = $qty * .05;} // if double sided times qry by .10 if($radio == "DOUBLE"){$total = $qty * .10;} //figure the tax.... $total = $total * $tax; //send the formated total amount back with two decimal places return number_format($total, 2); } // End of function. if (isset($_POST['q1'])) // Checks of radio button has a value { $Q1_Value = $_POST['q1']; // assigns value of radio button group q1 to a variable } // Minimal form validation: if ( is_numeric($_POST['quantity']) ) { // Print the heading: $success = 'Upload front side of card: <input name="image" accept="image/jpeg" type="file"> '; // Call the function, with or without tax: if (is_numeric($_POST['tax'])) { $sum = calculate_total ($_POST['quantity'], $_POST['tax'],$Q1_Value); } else { $sum = calculate_total ($_POST['quantity'], '1',$Q1_Value); //sends the value of one for tax if there is no tax value sent resulting in no tax. } } else { // Invalid submitted values. echo '<h1>Error!</h1> <p class="error">Please enter a valid quantity and price.</p>'; } // Leave the PHP section and create the HTML form: ?> <h1>Digital Studio Order Form</h1> <form action="calculator.php" method="post"> <p>Quantity: <select name="quantity"> <option value= "100" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 100) echo 'selected="selected"'; ?>>100</option> <option value= "250" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 250) echo 'selected="selected"'; ?>>250</option> <option value= "500" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 500) echo 'selected="selected"'; ?>>500</option> </select> </p> <p>Double sided: <INPUT TYPE="radio" NAME="q1" VALUE="DOUBLE" <?php if($Q1_Value == "DOUBLE"){ echo "CHECKED";} ?> > <!-- Above is first radio button in group "q1" the php script here checks if double was selected if so select it automatically --> <br /> <br /> Single Sided: <INPUT TYPE="radio" NAME="q1" VALUE="SINGLE" <?php if($Q1_Value != "DOUBLE"){ echo "CHECKED";} ?> > <!-- Above is second radio button in group "q1" the php script here checks if double was not selected. This is true if first time around (then double was thus not checked) if second time around and single was checked then double was thus not checked. --> <?php if (!empty($success)) { echo "<p class=\"messge\">" . $success . "</p>"; } ?> <p>Total: £<?php echo $sum?></p> <p><input type="submit" name="submit" value="Calculate!" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php // Include the footer: include ('includes/footer.html'); ?> Quote Link to comment 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.