Dimplistic Posted January 12, 2012 Share Posted January 12, 2012 Basically, I have a series of radio buttons (wanted checkboxes) were people select either Latte, Mocha or Cappuccino. I have included a hidden field for a price and using that to bring in the prices and add them up. <label for"Latte">Latte <br />€ 2.15</label> <br /> <input type="radio" name="coffee" id="Latte" value="Latte"> <input name="LattePrice" type="hidden" value="2.15"> </p> <p> <label>Mocha <br /> € 2.25</label> <br /> <input type="radio" name="coffee" id="Mocha" value="Mocha"> <input name="MochaPrice" type="hidden" value="2.25"> </p> <p> <label>Cappucino <br /> €2.35</label> <br /> <input type="radio" name="coffee" id="Cappucino" value="Cappucino"> <input name="CappPrice" type="hidden" value="2.35"> </p> The next section they choose the size they want Small, Medium or Large. <p> <label for="latteSmall">Small </label><img src="images/latte.png" alt="Small Latte" width="36" height="36"> <input type="radio" name="size" id="LatteSmall" value="Small"> <input name="SizePrice" type="hidden" value="0.00"> </p> <p> <label for="LatteMedium">Medium <br /> + € 0.25 </label> <img src="images/latte.png" alt="Medium Latte" width="42" height="42"> <input type="radio" name="size" id="LatteMedium" value="Medium"> <input name="SizePrice" type="hidden" value="0.25"> </p> <p> <label for="latteLarge">Large <br /> + € 0.45 </label> <img src="images/latte.png" alt="Large Latte" width="49" height="49"> <input type="radio" name="size" id="LatteLarge" value="Large"> <input name="SizePrice" type="hidden" value="0.45"> </p> I chose radio buttons to limit the users choice to one coffee and one size to make life easier (for myself). Ideally I wanted to use checkboxes but I could not get the php working. So basically, If Latte is selected echo it and the size. echo "Hi ". $name. ", " . "here is your order:" . "<br /> <br />"; echo "<strong>" . "Coffee:" . "</strong>" . "<br />" . $size . " " . $coffee . "<br /><br />"; That works fine but the pricing is coming out wrong as I was using: if ($coffee == "Latte") { $TotalCoffeePrice = $LattePrice + $SizePrice; echo "€ " . $TotalCoffeePrice . "<br /> <br />"; } elseif ($coffee == "Mocha") { $TotalCoffeePrice = $MochaPrice + $SizePrice; echo "€ " . $TotalCoffeePrice . "<br /> <br />"; } elseif ($coffee == "Cappucino") { $TotalCoffeePrice = $CappPrice + $SizePrice; echo "€ " . $TotalCoffeePrice . "<br /> <br />"; } What is happening here is if you select Latte and then Small it was adding Small, Medium and Large prices to the Latte. So instead of 2.15 I was getting 2.80 or something like that. Obviously when I recognised this I tried an array but that just went arseways!! Also, at the end of the php I was hoping to total up everything that they chose with: $TotalPrice = ($TotalCoffeePrice + $SizePrice + $MuffinPrice + $SamboPrice); echo "<strong>" . "Total Price:" . "</strong>" . "<br />" . "€ " .$TotalPrice . "<br /><br />"; However, this was giving a total of 9.80 even when nothing was selected! Quote Link to comment https://forums.phpfreaks.com/topic/254897-totally-new-to-php-need-urgent-help-with-form/ Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2012 Share Posted January 12, 2012 erm...why didn't you name your hidden fields for the sizes the same way you did for your types? Quote Link to comment https://forums.phpfreaks.com/topic/254897-totally-new-to-php-need-urgent-help-with-form/#findComment-1306965 Share on other sites More sharing options...
Dimplistic Posted January 12, 2012 Author Share Posted January 12, 2012 I have tried a few different things but can't seem to get it to work properly! Quote Link to comment https://forums.phpfreaks.com/topic/254897-totally-new-to-php-need-urgent-help-with-form/#findComment-1306966 Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2012 Share Posted January 12, 2012 what did you try in the way of arrays? oh, and is your form page an html page or a php one? Quote Link to comment https://forums.phpfreaks.com/topic/254897-totally-new-to-php-need-urgent-help-with-form/#findComment-1306970 Share on other sites More sharing options...
Dimplistic Posted January 12, 2012 Author Share Posted January 12, 2012 I made an attempt at an array but it gave me all sorts of error messages. For the names of each radiobutton I place [] at the end and in the php page I tried an if statement that echoed Latteprice + sizeprice[0] etc The form is on an html page and when submitted it goes to an order.php page. http://www.dairegoodwin.ie/auth2/slideform/ Quote Link to comment https://forums.phpfreaks.com/topic/254897-totally-new-to-php-need-urgent-help-with-form/#findComment-1306974 Share on other sites More sharing options...
Dimplistic Posted January 12, 2012 Author Share Posted January 12, 2012 Do you think it would be easier to leave out the hidden fields for the prices and just produces the prices in the php? Quote Link to comment https://forums.phpfreaks.com/topic/254897-totally-new-to-php-need-urgent-help-with-form/#findComment-1306975 Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2012 Share Posted January 12, 2012 that's what I'm thinkning. hard code your prices into an array with keys that = radio button values and then use them to work out the prices, something a little like: $pricelist_coffe = array('Latte'=> '2.15', 'Mocha'=> '2.25'....); ... $cost = 0; foreach($pricelist_coffe as $key => $value){ if ($key == $_POST['coffe']]){ $cost = $cost+$value; } } Quote Link to comment https://forums.phpfreaks.com/topic/254897-totally-new-to-php-need-urgent-help-with-form/#findComment-1306977 Share on other sites More sharing options...
PFMaBiSmAd Posted January 12, 2012 Share Posted January 12, 2012 If this was a real application, you would display the price on the form, but you would not pass the actual price through the form. You would only perform a calculation using price information that is completely server-side. Quote Link to comment https://forums.phpfreaks.com/topic/254897-totally-new-to-php-need-urgent-help-with-form/#findComment-1306978 Share on other sites More sharing options...
Dimplistic Posted January 12, 2012 Author Share Posted January 12, 2012 that's what I'm thinkning. hard code your prices into an array with keys that = radio button values and then use them to work out the prices, something a little like: $pricelist_coffe = array('Latte'=> '2.15', 'Mocha'=> '2.25'....); ... $cost = 0; foreach($pricelist_coffe as $key => $value){ if ($key == $_POST['coffe']]){ $cost = $cost+$value; } } Thanks for that. I tried using it but it didn't work. I am sure I am doing it wrong. Quote Link to comment https://forums.phpfreaks.com/topic/254897-totally-new-to-php-need-urgent-help-with-form/#findComment-1306989 Share on other sites More sharing options...
Dimplistic Posted January 12, 2012 Author Share Posted January 12, 2012 If this was a real application, you would display the price on the form, but you would not pass the actual price through the form. You would only perform a calculation using price information that is completely server-side. I think I will try removing the hidden fields containing the prices and see if I work it into the php page. Quote Link to comment https://forums.phpfreaks.com/topic/254897-totally-new-to-php-need-urgent-help-with-form/#findComment-1306990 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.