PDA

View Full Version : PHP Kirupa Mailer PHP - form help



saleekr
September 1st, 2008, 07:13 PM
Hi, i just downloaded this form and after looking around tried to make my own tweaks, however i keep getting errors.

here is my code

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

$to = "email@email.com";
$subject = "contact";
$name_field = $_POST['posName'];
$title_field = $_POST['posTitle'];
$company_field = $_POST['posComp'];
$addy_field = $_POST['posAddy'];
$phone_field = $_POST['posPhone'];
$email_field = $_POST['posEmail'];
$web_field = $_POST['posWeb'];
$message = $_POST['message'];
$desc = $_POST['describe'];
$other = $_POST['other'];
$ctime = $_POST['posTime'];


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

$body = "From: $name_field\n Title: $title_field\n Comapny: $company_field\n Address: $addy_field\n Phone: $phone_field\n E-Mail: $email_field\n Website: $web_field\n Request Reason: $check_msg\n Answer for Do you have a carpet question: $desc\n Answer when client chose Other: $other\n Best method of contact: $check_msg\n Best Time to Contact: $ctime\n";

mail($to, $subject, $body);
header("location: http://www.domain.com/thankyou.html");
die;

} else {
header("location:http://www.domain.com/error.html");
die;
}
?>

any ideas what i fugged up?

hl
September 1st, 2008, 08:41 PM
It helps when you post the errors, as well.

saleekr
September 1st, 2008, 08:49 PM
It helps when you post the errors, as well.

Sorry here is the error


Warning: Invalid argument supplied for foreach() in /usr/web/sites/domain.com/docroot/mailer.php on line 19

Warning: Invalid argument supplied for foreach() in /usr/web/sites/domain.com/docroot/mailer.php on line 22

Warning: Cannot modify header information - headers already sent by (output started at /usr/web/sites/domain.com/docroot/mailer.php:19) in /usr/web/sites/domain.com/docroot/mailer.php on line 29

Utech22
September 1st, 2008, 09:05 PM
edit: die();
What is the error that you are getting?

saleekr
September 1st, 2008, 09:35 PM
edit: die();
What is the error that you are getting?

Same error

simplistik
September 2nd, 2008, 10:34 AM
it's because $_POST['check'] and $_POST['contact'] are check boxes ( from the looks of your code ), so when you don't check them, you can't loop through them.

this one


Warning: Cannot modify header information - headers already sent by (output started at /usr/web/sites/domain.com/docroot/mailer.php:19) in /usr/web/sites/domain.com/docroot/mailer.php on line 29
i cause you're tryin to do a header redirect, but you've already passed your headers (<html></html>). So you need to put your php above your html OR make this your first line of your php which needs to be above all your html


ob_start();

tfg
September 3rd, 2008, 09:34 AM
the reason you're getting headers already sent is probably because php's error reporting has already output error messages to your browser, so disregard that warning for now. it's a bit like validating your xhtml, clearing one error will usually clear out a lot of those that follow it.

you're getting the invalid argument supplied error for one of a couple of reasons. i'm assuming (as is assumed in the post above) that $_POST['check'] is a checkbox? if that's the case, then the variable type will be boolean (true or false) and foreach() is a function which can only be used on arrays.

assuming this is the case, what you ideally need is something along the lines of this in your html:

<input type="checkbox" name="check" value="true" />

and your php would be something like this:

if($_POST['check'] == "true") {
$check_msg .= "Checked\n";
}

if you post your form html then it'd be easier to see what you're trying to achieve :)