Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Strings given in $_POST with php

Tags:

post

php

compare

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.

like image 485
helloandre Avatar asked Dec 06 '22 07:12

helloandre


2 Answers

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

?>
like image 117
Peter Bailey Avatar answered Dec 21 '22 06:12

Peter Bailey


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.

like image 38
Adam Avatar answered Dec 21 '22 06:12

Adam