Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Php and a way to case select

    I am new to php and use to program in visual basic. I am looking to see if there is a way to use a case select function. I am trying to practice by using three services that are offered - Service A, Service B and Service C. I have a textbox where once the user hits 'submit' it will show the total price for the services selected. The problem is that if they select multiple checkboxes it shows more than one textbox. I am sure there is a way to do it, but cannot find something like a case select function anywhere. I tried to use a break function at the end of each if statement, but it returned an error.
    Here is my code:

    <html>
    <head>
    <title>Services</title>
    </head>
    <body>
    <form action="" method="post">
    <table>
    <tr>
    <td>
    <input type="checkbox" name="service" value="a" />Service A</td>
    </tr>
    <tr>
    <td>
    <input type="checkbox" name="service" value="b" />Service B</td>
    </tr>
    <tr>
    <td>
    <input type="checkbox" name="service" value="c" />Service C</td>
    </tr>
    </table>
    <input type="submit" name="submit" value="submit">
    <?php
    if(isset($_POST['submit']))
    {
    $a = $_POST['service'];


    if($a == "a")
    {
    echo "<input type='text' name='txt' value='$1,000'/>";

    }
    if($a == "b")
    {
    echo "<input type='text' name='txt' value='$20,000'/>";

    }
    if($a == "c")
    {
    echo "<input type='text' name='txt' value='$300,000'/>";

    }

    if($a == "a" + "b")
    {
    echo "<input type='text' name='txt' value='$21,000'/>";

    }

    }
    ?>
    </form>
    </body>
    </html>

    Here is a picture of the problem: (In this case I have selected Service A and Service B.)
    Php and a way to case select-php-checkbox-problem.jpg

    Thanks in advance.

  • #2
    Regular Coder
    Join Date
    Aug 2006
    Location
    Richmond, CA
    Posts
    223
    Thanks
    3
    Thanked 11 Times in 10 Posts
    Quote Originally Posted by shane6741 View Post
    I am new to php and use to program in visual basic. I am looking to see if there is a way to use a case select function. I am trying to practice by using three services that are offered - Service A, Service B and Service C. I have a textbox where once the user hits 'submit' it will show the total price for the services selected. The problem is that if they select multiple checkboxes it shows more than one textbox. I am sure there is a way to do it, but cannot find something like a case select function anywhere. I tried to use a break function at the end of each if statement, but it returned an error.
    Here is my code:

    Thanks in advance.
    First, It's best practice to put code between code tags, and if it's specifically PHP, use PHP tags. Helps us reading it!

    Secondly, in PHP, this is called a switch. Here's the doc. It does exactly what you're looking for.

  • #3
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Daniel Israel View Post
    First, It's best practice to put code between code tags, and if it's specifically PHP, use PHP tags. Helps us reading it!

    Secondly, in PHP, this is called a switch. Here's the doc. It does exactly what you're looking for.
    Thanks, but I may not have been clear enough in my original post.

    I saw this earlier in the day but it didn't help because I cannot determine how to use a case where checkbox "Service A" and checkbox "Service B" are checked and then the prices are combined. I can create cases (case 0: Service A, case: 1 Service B, case 2: Service C), but how do I create a case where both checkbox A and checkbox B have been selected and then the total price is added and displayed inside the textbox?

    Thanks again in advance.

  • #4
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is an update. I have the case select working except for when I need two to be true. The first three work but not the last line Here is my case select code:

    <?php
    if(isset($_POST['submit']))
    {
    $a = $_POST['service'];


    if($a == "a")
    {
    echo "<input type='text' name='txt' value='$1,000'/>";

    }
    if($a == "b")
    {
    echo "<input type='text' name='txt' value='$20,000'/>";

    }
    if($a == "c")
    {
    echo "<input type='text' name='txt' value='$300,000'/>";

    }
    if(($a == 'a' and $a == 'b'))
    {
    echo "<input type='text' name='txt' value='$21,000'/>";

    }

    }
    ?>

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Ok first up thats not a case / switch. You're using multiple if () tests.

    You should use a switch:
    PHP Code:
    switch($a)
       {
       case 
    'a':
          
    //do something here
          
    break;
       case 
    'b':
          
    //do something here
          
    break;
       case 
    'c':
          
    //do something here
          
    break;
       default:
          
    //Default code if none of the above match
          
    break;
       } 
    Secondly you were asked to use php tags for the very reason that they format your code nicely (like above) so that we can read it easier.

    Thirdly you're doing this:
    if(($a == 'a' and $a == 'b'))

    How can a variable be both a and b at the same time? it can't. It CAN CONTAIN both (eg $a = 'ab'; ) but then your if / switch won't work anyway. Instead you'll need to use strpos() or similar to check if a string CONTAINS the value instead of EQUALLING the value. Also your brackets should be ((<code>) and (<code>)) - you're using ((<code> and <code>)). Well thats the way I'd do the brackets anyway.

    Fourthly (if there is such a word) you're also doing this:
    if(isset($_POST['submit']))

    Thats not good because internet explorer has a bug with submit buttons that isn't immediately obvious. See the link in my signature for my blog which contains an article about this bug and a video demonstration of it in action.
    Last edited by tangoforce; 07-09-2014 at 01:12 AM.
    I can't really think of anything to write here now...

  • #6
    Regular Coder
    Join Date
    Aug 2006
    Location
    Richmond, CA
    Posts
    223
    Thanks
    3
    Thanked 11 Times in 10 Posts
    as tango points out, you're naming the 3 checkboxes the same thing. If you want them to be additive, you can make them an array and circle through them adding the cost with a switch statement (similar to what tango has posted above). Looks something like so:

    PHP Code:
    <html>
    <head>
    <title>Services</title>
    </head>
    <body>
        <form action="" method="post">
            <table>
                <tr>
                    <td><input type="checkbox" name="service[]" value="a" />Service A</td>
                </tr>
                <tr>
                    <td><input type="checkbox" name="service[]" value="b" />Service B</td>
                </tr>
                <tr>
                    <td><input type="checkbox" name="service[]" value="c" />Service C</td>
                </tr>
            </table>
        <input type="submit" name="submit" value="submit">
    <?php
    if(isset($_POST['service']))
    {
        
    $services $_POST['service'];
        
    $cost 0;
        foreach (
    $services as $service) {
            switch (
    $service) {
                case 
    "a":
                    
    $cost $cost 1000;
                    break;
                case 
    "b":
                    
    $cost $cost 20000;
                    break;
                case 
    "c":
                    
    $cost $cost 300000;
                    break;
                default:
                    
    // not sure how we got here...
            
    }
        }
        if (
    $cost 0) {
            
    // note that I am just regurgitating the calculated value.  You can format any way you like
            
    echo "<input type='text' name='txt' value='" $cost "' />";
        }
    }
    ?>
        </form>
    </body>
    </html>
    I haven't tested this or anything, but this is rough example.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •