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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Noob needs help with very small problem

    Very new to php. I need to understand what I'm doing wrong here.
    Warning: Invalid argument supplied for foreach() in /home/fpforum/public_html/php/send.php on line 5

    Code:
    $radio1 = $_POST['group1'];
    $radio2 = $_POST['group2'];
    
    foreach($_POST[checkbox] as $box) {
            $box = $box + 0;
    }
    
    $total = $radio1 + $radio2 + $box; 
    
    Echo " <b>Total " . ($total) ." </b>";
    When i was looking at a tutorial they showed the foreach argument being used pretty much the same way i am using it. What am i doing wrong?
    Last edited by IZZO; 03-06-2009 at 05:55 AM.

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    You might miss to add highlighted:
    Code:
    $_POST['checkbox']
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I have tried it like that also. Still didn't work

  • #4
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    use:
    PHP Code:
    $totalbox++; 
    instead of
    PHP Code:
    $box $box 0
    orelse $box will reset on each loop

    PHP Code:
    <?php
    $radio1 
    $_POST['group1'];
    $radio2 $_POST['group2'];

    //make sure that it is an array
    if(is_array($_POST['checkbox']))
    {
       
    $totalbox 0;
       foreach(
    $_POST['checkbox'] as $box) {
               
    $totalbox++;
       }
    }

    $total $radio1 $radio2 $totalbox

    Echo 
    " <b>Total " . ($total) ." </b>";
    ?>
    use checkbox[] for your checkbox name

    And on your html form:
    Code:
    <form method="post">
    <input type="checkbox" name="checkbox[]" value="value1"/>
    <input type="checkbox" name="checkbox[]" value="value2"/>
    <input type="submit"/>
    </form>
    Last edited by kokjj87; 03-06-2009 at 05:30 AM.

  • #5
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you!
    It shows that error only when there isn't a checkbox checked off, But when at least one is checked it does the math just fine with out an error. How would i fix that?

  • #6
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    You need to make sure that it is set first...

    Replace:
    PHP Code:
    if(is_array($_POST['checkbox'])) 
    With:
    PHP Code:
    if(isset($_POST['checkbox']) && is_array($_POST['checkbox'])) 

  • Users who have thanked kokjj87 for this post:

    IZZO (03-06-2009)

  • #7
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you! Everything is working great now.
    I should have known about the if isset because i just saw something about it in the tutorial not to long ago.

    I had to use $box = $box + 0; still tho because when i used $totalbox++; it counted how many of the checkmarks where selected instead of adding the values together.


  •  

    Posting Permissions

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