Simon
Simon

Reputation: 121

php if statement not working

Is there any reason that my if statment below in php is not working? It does not calculate the price when the correct radio button is selected. It constantly displays price as "49"

     <td width="236" height="25" align="left">Booking Period:</td>
</tr>
            <tr>
                <td height="10" align="right" class="align_left">One Day: ₤49 </td>
                <td>
                  <input type="radio" name="duration" id="oneday" value="One Day Rental"/>
                </td>
              </tr>

    <tr>
                <td height="30" align="right" class="align_right">Two Day: ₤69</td>
                <td>
                  <input type="radio" name="duration" id="two" value="Two Day Rental"/>
                </td>
              </tr>
    <tr>
                <td height="30" align="right" class="align_right">Weekend: ₤79</td>
                <td>
                  <input type="radio" name="duration" id="weekend" value="Weekend Rental"/>
                </td>
              </tr>

            $price = 0;
            if ($duration=="oneday")
            $price = 49;
                elseif ($duration=="two")
            $price = 69;
            elseif ($duration=="weekend")
            $price = 79;
            else
                $price = 49;

        if (empty($_POST['extras'])) {
         $price = $price;
                } else { 
                if($extra == "Deodoriser"){
                            $price = $price + 7;
                        } elseif($extra == "Carpet Protector (5 litre)"){
                            $price = $price + 39;
                        } elseif($extra == "Carpet Repair Tools"){
                            $price = $price + 9;
                        } elseif($extra == "Furniture Moving Equipment"){
                            $price = $price + 7;
                        } elseif($extra == "Furniture Tabs"){
                            $price = $price + 2;
                        } elseif($extra == "Urine Decontamination Treatment"){
                            $price = $price + 17; }
                else
        $price = $price; 
        }

Upvotes: 0

Views: 5244

Answers (5)

Noor Khan
Noor Khan

Reputation: 520

just modify those lines on your code

            $price = 0;
        $duration = $_POST['duration'];
        if ($duration=="One Day Rental")
        {   $price = 49;    }
        elseif($duration=="Two Day Rental")
        {   $price = 69;    }
        elseif ($duration=="Weekend Rental")
        {   $price = 79;    }
        else
         {   $price = 49;   }

Upvotes: 2

nohero
nohero

Reputation: 82

By the way you shouldnt check the id. You must check for the value values of the html tags.

        $price = 0;
        if ($duration == "One Day Rental")
          $price = 49;
        else if ($duration == "Two Day Rental")
          $price = 69;
        else if ($duration == "....")
          $price = 79;
        else
          $price = 49;

Thats your problem I think...

By submitting

<input type="radio" name="duration" id="weekend" value="Weekend Rental"/>

in the Post array duration (the name) will be with its value (value), the id does not matter here.

Upvotes: 2

Nanne
Nanne

Reputation: 64399

  • I see no <?php ?> is that a paste error? If so, please fix :) Otherwise you should have them around your php.
  • check what $duration is. If that's a post variable, use it as $_POST[], not just like that.
  • Consider adding { and } around your ifs: it's a lot clearer what happens
  • $price=$price does nothing. WHy is that there?

Upvotes: 0

Emmerman
Emmerman

Reputation: 2343

You should use $_POST array as in if (empty($_POST['extras'])) {

if ($_POST['duration']=="oneday") ...

Upvotes: 2

Rudi Visser
Rudi Visser

Reputation: 21979

Does $_POST['extras'] exist and does it have values?? You should check with a var_dump($_POST['extras']) to see what contents are actually in there.

Otherwise, that code should work fine as far as I can tell.

Upvotes: 0

Related Questions