Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

IF statement from checkboxes

Options
  • 13-01-2012 12:17pm
    #1
    Registered Users Posts: 9,847 ✭✭✭


    Hiya,

    I have something like this based on 3 checkboxes:

    <html>
    checkbox name="whatever" value="Book1"
    checkbox name="whatever" value="Book2"
    checkbox name="whatever" value="Book3"
    </html>

    <?php
    $BookPrice == "3.50"
    if ($whatever== "Book1" && $whatever== "Book2")
    $Total = $BookPrice * 2
    {
    echo $BookPrice;
    }



    //This works fine, however with 3 checkboxes:


    $BookPrice == "3.50"
    if ($whatever== "Book1" && $whatever== "Book2" && $whatever== "Book3" )
    $Total = $BookPrice * 3
    {
    echo $BookPrice;
    }


    // I get the two results???
    ?>


Comments

  • Registered Users Posts: 9,847 ✭✭✭py2006


    Actually, even if only one checkbox is selected I get the result for 2 selected and 3 selected.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    That code is not right at all.

    [php]if ($whatever== "Book1" && $whatever== "Book2" && $whatever== "Book3" )
    $Total = $BookPrice * 3
    {
    echo $BookPrice;
    }[/php]

    From what I see.
    - Your HTML is wrong. You need
    <input type="checkbox" name="whatever" value="Book1">

    - Once that is fixed, since you've named them all the same, you can only pass one of them to the server. A variable can only have one value so your If is not going to work at all. How can the same variable have 3 different values.

    - You have $Total = $BookPrice * 3 after your if before the { } , so this means if that if will be true (Which it never will be), you'll execute that statement. And then you'll always execute what's in the { } regardless if the if is true or not. (Hence why you are seeing the echo twice since both if statements are incorrect.)

    - You are missing semi colons every where. Please correct your code. That code is very messy.


  • Registered Users Posts: 9,847 ✭✭✭py2006


    Webmonkey wrote: »
    That code is not right at all.

    [php]if ($whatever== "Book1" && $whatever== "Book2" && $whatever== "Book3" )
    $Total = $BookPrice * 3
    {
    echo $BookPrice;
    }[/php]

    From what I see.
    - Your HTML is wrong. You need
    <input type="checkbox" name="whatever" value="Book1">

    - Once that is fixed, since you've named them all the same, you can only pass one of them to the server. A variable can only have one value so your If is not going to work at all. How can the same variable have 3 different values.

    - You have $Total = $BookPrice * 3 after your if before the { } , so this means if that if will be true (Which it never will be), you'll execute that statement. And then you'll always execute what's in the { } regardless if the if is true or not. (Hence why you are seeing the echo twice since both if statements are incorrect.)

    - You are missing semi colons every where. Please correct your code. That code is very messy.

    Thanks for that. My actual code is slightly different and does have the semicolons etc. I just tried to simplify it here but forgot about them. Also my html is fine but I didn't type it up here correctly. Sorry.

    I see what you mean about the variable only having one value.

    I also tried it this way.

    [HTML]<input type="checkbox" name="whatever[]" value="Book1">
    <input type="checkbox" name="whatever[]" value="Book2">
    <input type="checkbox" name="whatever[]" value="Book3"> [/HTML]
    $pricelist_book = 11.99;
    
    if(isset($_POST['whatever']))
     
     {
    	for ($i=0; $i<count($_POST['whatever']);$i++) {
    		echo "<br />" . $_POST['whatever'][$i] . "<br /><br />". "(". "&#8364;". $pricelist_book . " " . "each" . ")"  ."<br /><br />";
    	}
     }
    

    That should (I hope) pick up whatever was selected from the checkboxes (some or all) and display the price after it. (same price for each).

    However, what I would like to see is for it to display the price of 2 books or 3 books if selected.


  • Registered Users Posts: 9,847 ✭✭✭py2006


    $price = count($_POST) * $bookPrice;

    might work


  • Registered Users Posts: 2,345 ✭✭✭Kavrocks


    py2006 wrote: »
    $price = count($_POST) * $bookPrice;

    might work
    Have you tried using integers as the value for $_POST instead of strings and just multiply $bookPrice * $_POST;?

    Its been a while since I've done anything in php but that's how I'd start off.


  • Advertisement
  • Registered Users Posts: 3,515 ✭✭✭arleitiss


    Where is your submit button?
    Also:
    $Total = $BookPrice * 2 lacks semicolon ( ; )
    $Total = $BookPrice * 2;


  • Registered Users Posts: 9,847 ✭✭✭py2006


    Thanks guys,

    The above code worked! :)


Advertisement