tags:

views:

217

answers:

3

I'm trying to do a very simple mail form in PHP but get the error:

PHP Notice: Undefined index: in /var/www/html/sites/send_contact.php on line 10, referer: http://example.com/contact2.php

My PHP file looks like this:

<?php // send_contact.php
// Contact subject
$subject =$_POST["$subject"]; 
// Details
$message=$_POST["$detail"];

// Mail of sender
$mail_from=$_POST["$customer_mail"]; 
// From 
$name2=$_POST["$name2"];

$header="From: $mail_from\r\n";

// Enter your email address
$to="[email protected]";

$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email 
// display message "We've recived your information"
if($send_contact){
echo "We've recived your contact information";
}
else {
echo "ERROR";
}
?>
+1  A: 

The error is telling you exactly what is wrong:

Line 10:

$name2=$_POST["$name2"];

You are using '$name2' before it is defined.

PHP can substitute variables in strings:

$var1 = "bar";
echo "foo $var1"; // prints "foo bar"

In your HTML use something similar to the following:

<input type="...whatever..." name="name2" />

Then in PHP, assuming the data was POSTed, you would access it using:

$name2 = $_POST["name2"];
Nat Ryall
that's exactly what i did , my $name2 = $_POST["name2"]; declaration is exactly the same as what you put.
Buffernet
In that case, your HTML hasn't got any field named "name2", or, at least, it's not posting with your form. Otherwise the POST superglobal would have a name2 key and you'd get no error. Is your page live? A link would be helpful.
Frank DeRosa
yeah its up at: http://dropcollective.com/beta/dropcollective/subpages/contact2.php
Buffernet
maybe i don't have an smtp server setup on my system?
Buffernet
As the error doesn't say anything about an SMTP server, I don't think that's the problem. As Kelix and Carlos are saying, you probably made a mistake defining the index in the $_POST array.
Douwe Maan
Joe, the difference between mine and yours is that my version has no '$' in front of 'name2' in '$_POST[]'. My post above explains why this does not work to some extent. You're problem is not the mail server - I repeat - the error is clearly pointing at what is wrong in this instance.
Nat Ryall
+1  A: 

The easiest guess is that you're doing a mistake accessing your variables.

instead of:

$name2=$_POST["$name2"];

use this:

$name2=$_POST["name2"];

Or, if you know the difference and are doing this on purpose, make sure your $name2 variable is defined with the correct name of the HTML form field.

As an aside, I would strongly recommend using a library like PHPMailer to send emails.
Your example is quite simple and the mail() should work just fine, but for anything more elaborate (ie. having attachments or html) or needing to send using an external mail server by SMTP (with or without authentication), it will do a much better job and save you lots of time.

For a better idea, check this example from their website:

require_once('../class.phpmailer.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on     errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP

try {
  $mail->Host       = "mail.yourdomain.com"; // SMTP server
  $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
  $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
  $mail->Username   = "yourname@yourdomain"; // SMTP account username
  $mail->Password   = "yourpassword";        // SMTP account password
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->AddAddress('[email protected]', 'John Doe');
  $mail->SetFrom('[email protected]', 'First Last');
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';     // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}
Carlos Lima
A: 

It seems that your code doesn't pass the $_POST data as you expect. I recommend you to check the keys of $_POST as follows:

<?php
print_r(array_keys($_POST));
?>

If it doesn't display the value of $name2, it means you should modify the web page sending form data to send_contact.php.

yinkyweb