Hello everyone,

I'm attempting to make a email contact form for my website but I keep getting an error that reads "Warning: Invalid argument supplied for foreach() in /nfs/[......]/contact_fa.php on line 15."

That points to this line right here:

foreach($_POST['check[]'] as $value) {
    $check_boxes .= $value." ";
}

This is supposed to post the proper value from this checkbox input in the form:

<input name="check[]" type="checkbox" id="tos" value="Agreed." />

I'm trying to have the value "Agreed" entered into the email via the line:

$body_message .= 'Agree to TOS: '.$check_boxes;

Can anyone help me understand why I'm getting this error?

Many thanks in advance!!

Recommended Answers

All 11 Replies

The array you're trying to access is $_POST['check']. You can use foreach($_POST['check'] as $value) or

$size_arr = count($_POST['check']);
for($i=0; $i < $size_arr; $i++) { ... }

The [...] after the variable is not necessary with PHP, so it's foreach($_POST['check'])

Just a little follow up I'm very new to PHP so some of this is going completely over my head.

I've tried changing the foreach($_POST['check'] as $value) (removing the brackets) and I'm still getting the error.

As for EvolutionFallen's solution I have absolutely no idea where to integrate that or how it works at all.

There is a point in this operation where the page reloads.
You need to check that your variables hold their intended value after the page has loaded.
It looks like the $_POST['check'] doesn't exist, or it is empty.
You can put:

if (isset($_POST)) {
    print_r($_POST); 
    die();
    }

at the top of your page and it will show you the value of the form variables when the page is reloaded.

So I posted Adam's code into the top of my PHP document and now the address just simply stops on the PHP document and displays only "Array ()" on the page and the email no longer sends.

I feel like adding that caused the values already in the array to be lost. Any other solutions?

For assistance, here's the entire PHP script:

<?php
$field_name = $_POST['fa_name'];
$field_age = $_POST['fa_age'];
$field_email = $_POST['fa_email'];
$drop_down_country = $_POST['fa_country'];
$field_instrument = $_POST['fa_instrument'];
$field_playingexp = $_POST['fa_playingexp'];
$field_style = $_POST['fa_style'];
$field_video = $_POST['fa_video'];
$field_additionalvideo1 = $_POST['fa_addvid1'];
$field_additionalvideo2 = $_POST['fa_addvid2'];
$field_additionalvideo3 = $_POST['fa_addvid3'];
$field_soundcloud = $_POST['fa_soundcloud'];
$field_questionresp = $_POST['fa_questionresp'];
foreach($_POST['check'] as $value) {
    $check_boxes .= $value." ";
}

$mail_to = 'example@emailaddress.com';
$subject = 'Featured Artist Program Application '.$field_name;

$body_message = 'Name: '.$field_name."\n";
$body_message .= 'Age: '.$field_age."\n";
$body_message .= 'Email: '.$field_email."\n";
$body_message .= 'Country: ' .$drop_down_country."\n";
$body_message .= 'Instrument: '.$field_instrument."\n";
$body_message .= 'Playing Experience: '.$field_playingexp."\n";
$body_message .= 'Style and Influences: '.$field_style."\n";
$body_message .= 'Video Clip: '.$field_video."\n";
$body_message .= 'Additional Video Clip 1: '.$field_additionalvideo1."\n";
$body_message .= 'Additional Video Clip 2: '.$field_additionalvideo2."\n";
$body_message .= 'Additional Video Clip 3: '.$field_additionalvideo3."\n";
$body_message .= 'Soundcloud: '.$field_soundcloud."\n";
$body_message .= 'Why Are You a Good Candidate: '.$field_questionresp."\n";
$body_message .= 'Agree to TOS: '.$check_boxes;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        window.location = 'http://www.sample.com/';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Email Message failed. Please, send an email directly to us at sample@emailaddress.com');
        window.location = 'mailto:sample@emailaddress.com';
    </script>
<?php
}
?>

there's is nothing wrong with your code now if you followed their suggestions, try printing out the checkbox first to see if it has values or not, use print_r(), like

print_r($_POST['check']);

and make sure it's POST not GET and try posting here your html form here, so we can see if you mad some mistakes there

Here's my HTML form. Actual urls have been omitted for privacy's sake. It is labeled as "post" so everything should be getting there.

<form name="featuredartistapp" enctype="text/plain" method="post" action="http://www.sample.com/contact_fa.php">
  <table width="100%" border="0" cellspacing="5" cellpadding="0">
    <tr>
      <td width="33%" align="right">Name:</td>
      <td colspan="2">
      <input name="fa_name" type="text" id="name" size="30" /></td>
    </tr>
    <tr>
      <td align="right">Age:</td>
      <td colspan="2"><input type="text" name="fa_age" size="3" /></td>
    </tr>
    <tr>
      <td align="right">Email Address:</td>
      <td colspan="2"><input type="text" name="fa_email" size="35" /></td>
    </tr>
    <tr>
      <td align="right">Country of Residence:</td>
      <td colspan="2"><select name="fa_country">
        <option>Please Select...</option>
        <option value="US">United States</option>
        <option value="CA">Canada</option>
        <option>[further options omitted for length]</option>
</select></td>
    </tr>
    <tr>
      <td align="right">Instrument Used:</td>
      <td colspan="2"><input type="text" name="fa_instrument" size="30" /></td>
    </tr>
    <tr>
      <td align="right">Please Describe Your Playing Experience:</td>
      <td colspan="2"><textarea name="fa_playingexp" id="playingexp" cols="65" rows="5"></textarea></td>
    </tr>
    <tr>
      <td align="right">Briefly Describe your musical style and influences.</td>
      <td colspan="2"><textarea name="fa_style" id="style" cols="65" rows="5"></textarea></td>
    </tr>
    <tr>
      <td align="right">Video Link:</td>
      <td colspan="2"><input type="text" name="fa_video" size="35" /></td>
    </tr>
    <tr>
      <td align="right">Additional Video #1:</td>
      <td colspan="2"><input type="text" name="fa_addvid1" size="35" /></td>
    </tr>
    <tr>
      <td align="right"><p>Additional Video #2:</p></td>
      <td colspan="2"><input type="text" name="fa_addvid2" size="35" /></td>
    </tr>
    <tr>
      <td align="right">Additional Video #3:</td>
      <td colspan="2"><input type="text" name="fa_addvid3" size="35" /></td>
    </tr>
    <tr>
      <td align="right">Soundcloud Profile (Optional):</td>
      <td colspan="2"><input type="text" name="fa_soundcloud" size="35" /></td>
    </tr>
    <tr>
      <td align="right">Please briefly state why you believe you are an ideal candidate to become a Featured Artist.</td>
      <td colspan="2"><textarea name="fa_questionresp" id="questionresp" cols="65" rows="5"></textarea></td>
    </tr>
    <tr>
      <td align="right">&nbsp;</td>
      <td width="34%">&nbsp;</td>
      <td width="33%">&nbsp;</td>
    </tr>
    <tr>
      <td colspan="3" align="center">I have read and agree to the Featured Artist Application Terms of Service: 
        <input name="check[]" type="checkbox" id="tos" value="Agreed." />
      </td>
    </tr>
    <tr>
      <td align="right">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td align="right">&nbsp;</td>
      <td align="center"><input name="submit" type="submit" id="submit" value="Submit" /></td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>

now that makes sense why your getting an error, why do you have to make the checkbox as an array? if you only had one checkbox? changed your checkbox
from

 <input name="check[]" type="checkbox" id="tos" value="Agreed." />

to

<input name="check" type="checkbox" id="tos" value="Agreed." />

then into your php code
from

foreach($_POST['check'] as $value) {
    $check_boxes .= $value." ";
}

to

$check_boxes = $_POST['check'];

The email sends, I no longer get an error, and the page redirects properly, however the values are not submitted into the email and I get a message reading:

"Name:
Age:
Email:
Country:
Instrument:
Playing Experience:
Style and Influences:
Video Clip:
Additional Video Clip 1:
Additional Video Clip 2:
Additional Video Clip 3:
Soundcloud:
Why Are You a Good Candidate:
Agree to TOS: "

No inserted values. Am I missing the action that posts that information into the email?

I ran your code and the $_POST var was empty.
I removed the 'enctype' declaration and the$ _POST var was not empty.
<form name="featuredartistapp" enctype="text/plain" method="post"
print_r() is a function that dispays the contents of an array, when the array is empty, print_r returns 'Array()'
I've never used that enctype declaration and I don't know why it is a problem here.
Your PHP code on the action page assumes $_POST will be defined, this is either lazy or a mistake:

<?php
$field_name = $_POST['fa_name'];
$field_age = $_POST['fa_age'];

I recommend wrapping the whole page in an if statement:

if (isset($_POST['submit'])){ // $_POST has data...
    $field_name = $_POST['fa_name']; //assign variables
    mail(); // send mail
} else { // $_POST is not set
    die("form not submitted aborting...");
}

So I posted Adam's code into the top of my PHP document and now the address just simply stops on the PHP document and displays only "Array ()" on the page and the email no longer sends.

Of course the command 'die()' will stop your script running, what's the point in sending an empty mail? Get the variables into the $_POST var and then move on to the mail sending.

I feel like adding that caused the values already in the array to be lost.

Haha, probably best not to :D

Use error checking, always, for everything!

At long last it's FINALLY working properly!!

Thanks everybody for your help and patience, particularly with my inexperience and lack of knowledge! I appreciate it greatly!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.