ianco Posted January 30, 2010 Share Posted January 30, 2010 Hi all I wonder if you can tell me where i am going wrong. I have a form where i want to add together the price of food (for example). On hitting the submit button I don't get an output. I may be way out but i've shown what i have so far. Thanks Ian <FORM NAME ="form1" METHOD ="POST" ACTION ="testy.php"> <Input type = 'Checkbox' Name ='melon' value ="net" > melon<br> <Input type = 'Checkbox' Name ='pear' value ="met" > pear<br> <Input type = 'Checkbox' Name ='chicken' value ="let" > chicken<br> <Input type = 'Checkbox' Name ='pork' value ="ket" > pork<br> <INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Submit"><br> </FORM> <?php $melon = ' '; $pear = ' '; $chicken = ' '; $pork = ' '; if (isset($_POST['Submit1'])) { if (isset($_POST['melon'])) { $melon = $_POST['melon']; if ($melon = 'net') { $melon = "1"; } } if (isset($_POST['pear'])) { $pear = $_POST['pear']; if ($pear = 'met') { $pear = "2"; } } if (isset($_POST['chicken'])) { $chicken = $_POST['chicken']; if ($chicken = 'let') { $chicken = "2.2"; } } if (isset($_POST['pork'])) { $pork = $_POST['pork']; if ($pork = 'ket') { $pork = "2.5"; } } } $start = array($melon,$pear); $main = array($chicken,$pork); function sum() { global $start,$main; $total = $start + $main; echo $total; } ?> Link to comment https://forums.phpfreaks.com/topic/190349-adding-costs-using-php-form/ Share on other sites More sharing options...
wildteen88 Posted January 30, 2010 Share Posted January 30, 2010 Your code will not output anything as all you're doing is defining a bunch of variables based on what was selected in your form. You have defined a function called sum which is not being called. Link to comment https://forums.phpfreaks.com/topic/190349-adding-costs-using-php-form/#findComment-1004209 Share on other sites More sharing options...
ianco Posted January 30, 2010 Author Share Posted January 30, 2010 ok so I add sum(); It outputs Array still learning php i'm afraid Link to comment https://forums.phpfreaks.com/topic/190349-adding-costs-using-php-form/#findComment-1004210 Share on other sites More sharing options...
wildteen88 Posted January 30, 2010 Share Posted January 30, 2010 To add up arrays use array_sum. Also when use variables with functions you should pass them as paramaters. Making functions use variables in the global scope is bad practice. $start = array($melon,$pear); $main = array($chicken,$pork); sum($start, $main) function sum($start, $main) { $total = array_sum($start) + array_sum($main); echo $total; } Link to comment https://forums.phpfreaks.com/topic/190349-adding-costs-using-php-form/#findComment-1004215 Share on other sites More sharing options...
Locked Posted January 30, 2010 Share Posted January 30, 2010 if (isset($_POST['Submit1'])) { $cost = array(); if (isset($_POST['melon'])) { ($_POST['melon'] = 'net') ? $cost['melon'] = '1' : $cost['melon'] = FALSE; } if (isset($_POST['pear'])) { ($_POST['pear'] = 'met') ? $cost['pear'] = '2' : $cost['pear'] = FALSE; } if (isset($_POST['chicken'])) { ($_POST['chicken'] = 'let') ? $cost['chicken'] = '2.2' : $cost['chicken'] = FALSE; } if (isset($_POST['pork'])) { ($_POST['pork'] = 'ket') ? $cost['pork'] = '2.5' : $cost['pork'] = FALSE; } } echo 'It will cost you '.array_sum($cost); I used short if statments in the example http://davidwalsh.name/javascript-shorthand-if-else-examples Im not sure why you set the variables at the top of the script, i dont think its needed. Link to comment https://forums.phpfreaks.com/topic/190349-adding-costs-using-php-form/#findComment-1004225 Share on other sites More sharing options...
ianco Posted January 30, 2010 Author Share Posted January 30, 2010 That's done the job wildteen88, thanks very much I'll check out the short code later Ian Link to comment https://forums.phpfreaks.com/topic/190349-adding-costs-using-php-form/#findComment-1004226 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.