contact form in PHP

I have a contact form in PHP that gives me some errors.This is the code I use:

$message=$_POST["name"]."rn".$_POST["email"]."rn".$_POST["phone"]."rn".$_POST["subject"]."rn".$_POST["message"];

smtpmailer('ali_n_claudiu@yahoo.com', 'mailinteraction@gmail.com', 'Improve Mediehus Aarhus', 'From contact form', $message);

My problem is that when I load the page for the first time it gives me errors regarding the "$message=..." row for all the strings:

Notice: Undefined index: name in D:xampphtdocsinteractioncontact.php on line 56
Notice: Undefined index: email in D:xampphtdocsinteractioncontact.php on line 56 and same error for phone, subject and message.

after I submit the contact form, I don't receive anymore errors.

also when I load the page for the first time, it sends and empty e-mail to the desired address

last problem is that even if I use a javascript code for receiving a pop-up message after submiting the email, I don't receive any.

I read other posts and tried different ideas that you gave other people with similar errors, but without any result.


You should make use of the isset construct to check for variables before assigning.

if(isset($_POST["name"]))
{
$name = $_POST["name"];
}
else
{
echo "Name is not set";
}

You should do this for all if your other $_POST variables before passing it to your smtpmailer() .


Button Code

<input type="submit" name="submit">

and replace the code

if(isset($_POST['submit'])){

$message=$_POST["name"]."rn".$_POST["email"]."rn".$_POST["phone"]."rn".$_POST["subject"]."rn".$_POST["message"];

smtpmailer('ali_n_claudiu@yahoo.com', 'mailinteraction@gmail.com', 'Improve Mediehus Aarhus', 'From contact form', $message);

}

TO

$message=$_POST["name"]."rn".$_POST["email"]."rn".$_POST["phone"]."rn".$_POST["subject"]."rn".$_POST["message"];

smtpmailer('ali_n_claudiu@yahoo.com', 'mailinteraction@gmail.com', 'Improve Mediehus Aarhus', 'From contact form', $message);

Use isset as @Shankar Damodaran said or

Try this if you want hide notice and errors.

ini_set("display_errors","off"); //set it on if you want display errors

链接地址: http://www.djcxy.com/p/69714.html

上一篇: 包括标题页会话错误

下一篇: 在PHP中联系表单