I have a form that is sending in sizes of things, and I need to see what the strings are equal to so that I can set the price accordingly. When i try to do this, it says that they are not equal, and i get no prices. This is the code i'm using:
if ($_POST['sizes'] == "Small ($30)"){$total = "30";}
if ($_POST['sizes'] == "Medium ($40)"){$total = "40";}
if ($_POST['sizes'] == "Large ($50)"){$total = "50";}
else {$total = $_POST['price'];}
What am i doing wrong here? I can echo $_POST['sizes'] and it gives me exactly one of those things.
What Paul Dixon said is correct. Might I also recommend using a switch statement instead of that clunky chunk of if statements (which actually has a logic bug in it, I might add - $total
will always equal $_POST['price']
when not 'Large ($50)'
)
<?php
switch ( $_POST['sizes'] )
{
case 'Small ($30)' :
$total = 30;
break;
case 'Medium ($40)' :
$total = 40;
break;
case 'Large ($50)' :
$total = 50;
break;
default:
$total = $_POST['price'];
break;
}
?>
That's a good candidate for a switch/case statement, with your 'else' being a default.
Also, without using elseif's on Medium and Large, if your $_POST['sizes'] is not Large, then your $total will always be $_POST['price']. This could be throwing you off as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With