PDA

View Full Version : PHP Mailer question.



Soig
December 5th, 2006, 04:40 PM
Hey Guys, I'm having a hard time with a simple code. I got this from a website and I only eddited what I needed to. But wehn I use it, it gives me this result



Warning: Invalid argument supplied for foreach() in /home/u2xs/public_html/magnum/php/mailer.php on line 11

The Entire Code is


<?php
if(isset($_POST['submit'])) {

$to = "sergio@u2xs.com";
$subject = "A question from $name at MT.com";
$name = $_POST['name'];
$company = $_POST['company'];
$eMail = $_POST['eMail'];
$telephone = $_POST['telephone'];
$message = $_POST['message'];
foreach($_POST['check'] as $value) {
$check_msg .= "Checked: $value\n";
}

$body = " Name: $name\n E-Mail: $eMail\n Company: $company\n Telephone: $telephone\n Message: $message";

echo "<HTML><body bgcolor=0C2776 text=white><font size=7>Thank you</font></body</html>";
mail($to, $subject, $body);

} else {
echo "blarg!";
}
?>

The part I believe has a mistake is this one...


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

Any thoughts??? Thanks

Seb Hughes
December 6th, 2006, 02:07 AM
Has to be an array the virst value in the foreach i think.

foreach (array_expression as $value)
statement

Danii
December 6th, 2006, 07:05 AM
Usually i check if its an array im dealing with before performing a foreach loop



if( is_array($array) )
foreach($array as $var) {
//Do stuff
}
else
echo "$array not an array.";

Soig
December 7th, 2006, 06:42 PM
Thanks for the help. I think it would have made sense on my part to inform you that I don't know anything about PHP. I've tried the code that each of you gave by cutting and pasting but neither worked.

Is it possible to ask for a functional piece? Thanks

Danii
December 8th, 2006, 04:46 AM
are you sure your posting the data right.
try this and see what the result is.


echo '<pre>';
print_r( $_POST['check'] );
echo '</pre>';


if that really is an array, that little piece of code should show you all the values stored in it.

Soig
December 13th, 2006, 01:28 PM
Hey Danii, thanks. I tried it out but still got this: Since it did not work, I've since changed it back.

Parse error: syntax error, unexpected $end in /home/u2xs/public_html/magnum/php/mailer.php on line 19

I don't know if it makes it easier to understand but the site is Http://www.u2xs.com/magnum/contact.html.

I've uploaded a copy of the PHP (but in TXT format) onto this site just in case: http://www.u2xs.com/temp/mailer.txt

As always, thank you. Your help is greatly appreciated. I can interrupt from bashing my head against the wall! :)

Refined
December 13th, 2006, 06:36 PM
Hello <strong>Soig</strong>.

This is my first post on the infamous Kirupa forums and I hope I can help you!

The "unexpected $end " error you are receiving means you have failed to close a statement or loop etc.

This is how an if statement needs to look;



if ( statement ) {
statement is true, do something;
}


You therefore need to close your if statement with a }.

Could you post what HTML code you are using for the form which submits this data? We need to make sure that $_POST['check'] is an array. If you are trying to detect whether or not a checkbox has been ticked in the form, you can use this code;



if ( isset( $_POST['checkbox_name'] ) ) {
checkbox is checked;
} else {
checkbox is unchecked;
}


Unles you have dynamically created check boxes, you shouldn't need to use a 'foreach' loop.

Hope this helps in some obsure way.