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

    Invalid argument error in foreach()

    Hi Everyone,

    Thanks for reading my post. I am new to PHP and wrote a script to process a simple volunteer form with checkboxes. Everything works fine if atleast one check box is checked. However, if they are all left unchecked, I get an invalid argument error. Here is the code I am using:

    PHP Code:

    $to 
    "me@me.com";
    $subject "Volunteer Form";
    $firstname $_POST['firstname'];
    $lastname $_POST['lastname'];
    $email$_POST['email'];
    $comments $_POST['comments'];

    foreach(
    $_POST['check'] as $value) {
    $check_msg .= "Checked: $value\n";
    }

    $body "First Name: $firstname\nLast Name: $lastname\nE-Mail: $email\nComments: $comments\n$check_msg";

     
    if (empty(
    $email) || empty($firstname)) {
        
    header"Location: http://www.example.com/error.html" );}
    else {

    mail($to$subject$body);
    header"Location: http://www.example.com/thankyouvolunteer.html" );

    I really appreciate any advice anyone has. I know this is probably really simple, but it has tortured me for 3 hours.

    THANKS!
    Last edited by MattB; 03-17-2010 at 03:57 PM. Reason: Update code snippet

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    Enclose all the above code inside an if statement, like
    PHP Code:
    if(isset($_POST['firstname'])){
    // all the code to process POST data goes here.


    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    MattB (03-17-2010)

  • #3
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by abduraooft View Post
    Enclose all the above code inside an if statement, like
    PHP Code:
    if(isset($_POST['firstname'])){
    // all the code to process POST data goes here.


    Shouldn't that be !empty() instead of isset()?
    Are you a Help Vampire?

  • #4
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, I figured it out! Thanks so much Abduraooft!

    Here is the revised code:

    PHP Code:
    <?php
    if(isset($_POST['check'])){

    $to "me@me.com";
    $subject "Volunteer Form";
    $firstname $_POST['firstname'];
    $lastname $_POST['lastname'];
    $email$_POST['email'];
    $comments $_POST['comments'];

    foreach(
    $_POST['check'] as $value) {
    $check_msg .= "Checked: $value\n";
    }

    $body "First Name: $firstname\nLast Name: $lastname\nE-Mail: $email\nComments: $comments\n$check_msg";

     
    if (empty(
    $email) || empty($firstname)) {
        
    header"Location: http://www.example.com/error.html" );}
    else {

    mail($to$subject$body);
    header"Location: http://www.example.com/thankyouvolunteer.html" );

    }else{

    $to "me@me.com";
    $subject "Volunteer Form";
    $firstname $_POST['firstname'];
    $lastname $_POST['lastname'];
    $email$_POST['email'];
    $comments $_POST['comments'];


    $body "First Name: $firstname\nLast Name: $lastname\nE-Mail: $email\nComments: $comments";

     
    if (empty(
    $email) || empty($firstname)) {
        
    header"Location: http://www.example.com/error.html" );}
    else {

    mail($to$subject$body);
    header"Location: http://www.example.com/thankyouvolunteer.html" );
    }
    }
    ?>

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    >.<
    You're both looking at different things; abduraooft is pointing to ensure that the data is posted, while tomws is looking at validation.
    You're actually always getting errors, some you see some you don't. The error is likely on the $check_msg since if nothing is checked the variable never exists, while if one is checked you're still getting an error indicating you're concatinating on an invalid variable. So its lose lose.
    Initialize $checked_msg prior to the foreach loop as an empty string with $checked_msg = '';. Validate you're data as necessary as well, and wrap the entire block in a check to ensure that its been posted. There is no guarentee that any of this data has been passed, so you may want to add additional error checking to ensure it (though you have started with the check on empty for email and firstname). Using isset will determine if something exists, while using empty will check if it has content. You can get around isset calls by assigning by reference instead, but if you don't know what I mean when I say that don't try.

    Edit:
    Also, the call against a header for a redirect still continues processing this script. This will not create errors in you're current code since its controlled with if/else combinations with no trailing processing, but you should consider adding an exit() call after you're headers.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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