Jump to content

Invalid argument supplied for foreach() - Problem with checkboxes in a form


matthewread

Recommended Posts

Hello,

 

I am totally new to PHP so forgive me if I'm missing something completely obvious.

 

I created a contact form which e-mails the data from the form through to me.

 

I made the form based on a tutorial on kirupa.com and it originally worked properly. I have just updated the form by adding some more text fields and some check boxes.

 

I made the check boxes based on the explanation here - http://www.kirupa.com/web/php_contact_form3.htm

 

The checkboxes work if they are checked and the form submits properly and takes the user to another page, thanks.html which contains just a short message thanking the user for getting in touch.

 

If the checkboxes are unchecked, however, the form still submits but the user will get taken to an error page saying:

 

Warning: Invalid argument supplied for foreach() in /home/hmarket/public_html/mailer.php  on line 15

 

How can I fix this so that when the checkbox is unchecked, the user is still taken through to the thanks.html page?

 

Here is the php code on my mailer.php file:

 

<?php
if(isset($_POST['submit'])) /*If the submit button was pressed...*/ { 
$to = "matthewread@huargemarketing.com"; /*Recipient E-mail Address*/
$subject = "Huargemarketing.com Contact Form"; /*E-mail Subject Line*/

$name_field = $_POST['name'];
$email_field = $_POST['email'];
$telno_field = $_POST['telno'];
$busname_field = $_POST['busname'];
$busbased_field = $_POST['busbased'];
$bustype_field = $_POST['bustype'];
$radio = $_POST['radio'];
$msg = $_POST['msg'];

foreach($_POST['check'] as $value) {
$check_msg .= "Services I am interested in: $value\n";
}
   
$body  =  "\nFrom: $name_field\n E-Mail: $email_field\n Telephone Number: $telno_field\n Business Name: $busname_field\n Where is your business based? $busbased_field\n Business Type: $bustype_field\n Do you have a website your business? $radio\n $check_msg\n Additional Information / Comments:\n $msg";

$URL="/thanks.html";

mail($to, $subject, $body);

} else {

echo "Error! Please contact matthewread@huargemarketing.com";

}
?>

 

 

Here is my html code for the contact form:

<div id="formbox">
  <p>If you are interested in any of our services or would like to ask us any questions, please use the contact form below. </p>
  <p> </p>
  <p><font color="#FF0000">* = Required Field</font></p>
  <p> </p>
  <form id="form" name="form" method="post" action="mailer.php" onsubmit="return verify();">
    <table width="400" border="0" align="left" cellpadding="10">
      <tr>
        <td width="194">* Name:</td>
        <td width="130"><label>
          <input name="name" type="text" id="name" size="25" maxlength="40" />
        </label></td>
      </tr>
      <tr>
        <td>* E-mail Address:</td>
        <td><input name="email" type="text" id="email" size="25" maxlength="40" /></td>
      </tr>
      <tr>
        <td>* Contact Telephone Number:</td>
        <td><label>
          <input name="telno" type="text" id="telno" size="25" maxlength="11" />
        </label></td>
      </tr>
      <tr>
        <td><p>* Business Name:</p></td>
        <td><label>
          <input name="busname" type="text" id="busname" size="25" maxlength="40" />
        </label></td>
      </tr>
      <tr>
        <td>* Where is your business based?</td>
        <td><input name="busbased" type="text" id="busbased" size="25" maxlength="40" /></td>
      </tr>
      <tr>
        <td>* Type of Business:</td>
        <td><label>
          <input name="bustype" type="text" id="bustype" size="25" maxlength="40" />
        </label></td>
      </tr>
      <tr>
        <td height="46">* Do you  have a  website?</td>
        <td><p>
          <label>
            <input type="radio" value="Yes" name="radio" />
            Yes</label>
          <br />
          <label>
            <input type="radio" value="No" name="radio" />
            No</label>
          <br />
        </p></td>
      </tr>
      <tr>
        <td valign="top">Which service(s) are you interested in?</td>
        <td><p>
          <label>
            <input name="check[]" type="checkbox" value="Rent a Site" />
          </label>
          Rent a Site</p>
          <p>
            <label>
              <input type="checkbox" name="check[]" value="SEO" />
            </label>
            SEO</p>
          <p>
            <label>
              <input type="checkbox" name="check[]" value="Web Design" />
            </label>
            Web Design
          </p></td>
      </tr>
      <tr>
        <td valign="top">Additional Information / Comments:</td>
        <td><label>
          <textarea name="msg" cols="20" rows="5" id="msg"></textarea>
        </label></td>
      </tr>
      <tr>
        <td height="34"> </td>
        <td align="right"><label>
          <input type="submit" name="submit" id="submit" value="Submit" align="right" />
        </label></td>
      </tr>
    </table>
  </form>
</div>

 

Thanks.

Link to comment
Share on other sites

Change this:

foreach($_POST['check'] as $value) {
        $check_msg .= "Services I am interested in: $value\n";
}

 

To this:

if(isset($_POST['check']) && is_array($_POST['check']))
{
    foreach($_POST['check'] as $value)
    {
        $check_msg .= "Services I am interested in: $value\n";
    }
}

 

The problem is the code is trying to run a foreach() loop on a variable ($_POST['check']) that does not exist. Also, added validation to ensure it is an array as well. So, with the new code, if the value doesn't exist or if it is not an array, the foreach() is skipped.

Link to comment
Share on other sites

Change this:

foreach($_POST['check'] as $value) {
        $check_msg .= "Services I am interested in: $value\n";
}

 

To this:

if(isset($_POST['check']) && is_array($_POST['check']))
{
    foreach($_POST['check'] as $value)
    {
        $check_msg .= "Services I am interested in: $value\n";
    }
}

 

The problem is the code is trying to run a foreach() loop on a variable ($_POST['check']) that does not exist. Also, added validation to ensure it is an array as well. So, with the new code, if the value doesn't exist or if it is not an array, the foreach() is skipped.

 

Thanks for the help. I just tried this, it still submits the form but does not take the user to "thanks.html" :(

Link to comment
Share on other sites

And, where does it "take" them. Are you getting any errors?

 

Please understadn in the first problem you had it isn't "taking" them to another page. The mailer.php script encounter an error so it displayed the error on the page and stoped processign any more code (inlcuding the header to redirect to the thanks page).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.