Jump to content

Recommended Posts

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

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;
}

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.