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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

IF statement from checkboxes

  • 13-01-2012 11:17am
    #1
    Registered Users, Registered Users 2 Posts: 9,844 ✭✭✭


    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, Registered Users 2 Posts: 9,844 ✭✭✭py2006


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


  • Registered Users, Registered Users 2 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, Registered Users 2 Posts: 9,844 ✭✭✭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, Registered Users 2 Posts: 9,844 ✭✭✭py2006


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

    might work


  • Registered Users, Registered Users 2 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, Registered Users 2 Posts: 3,515 ✭✭✭arleitiss


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


  • Registered Users, Registered Users 2 Posts: 9,844 ✭✭✭py2006


    Thanks guys,

    The above code worked! :)


Advertisement