联系表。一切都应该有效,但事实并非如此

时间:2013-03-19 02:53:21

标签: php html forms contact

我的联系表格有问题。当我在我的网站上填写它时,我会收到一封电子邮件,但字段:姓名,电子邮件和邮件在我收到的电子邮件中仍然是空白的。

我的联系人群包括:contacts.html(我网站上的html页面); contactengine.php(实际形式); thanks.html(成功发布问题后会打开); unsuccessful.html(不成功时打开)。

有谁能告诉我这段代码有什么问题?

CONTACTS.HTML

<form id="form" form action="contactengine.php" method="post" >
   <fieldset>
     <label><input type="text" name="cf_name" value="Name" id="cf_name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''"></label>
     <label><input type="text" name="cf_email" value="e-Mail" id="cf_email" onBlur="if(this.value=='') this.value='e-Mail'" onFocus="if(this.value =='e-Mail' ) this.value=''"></label  
     <label><textarea name="cf_message" id="cf_message" onBlur="if(this.value==''){this.value='Message'}" onFocus="if(this.value=='Message'){this.value=''}">Message</textarea></label>
     <div class="btns">
     <a href="contacts.html" class="link-1">Cancel</a>
     <a href="contactengine.php" class="link-1" onClick="document.getElementById('form').submit()">Submit</a></div>
   </fieldset>  
</form>

CONTACTENGINE.PHP

<?php

$EmailFrom = "form@mysite.rs";
$EmailTo = "office@mysite.rs";
$Subject = "Message from website";
$cf_name = Trim(stripslashes($_POST['Name'])); 
$cf_email = Trim(stripslashes($_POST['Email'])); 
$cf_message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) {
   print "<meta http-equiv=\"refresh\" content=\"0;URL=unsuccessful.html\">";
   exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $cf_name;
$Body .= "\n";
$Body .= "e-Mail: ";
$Body .= $cf_email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $cf_message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=unsuccessful.html\">";
}
?>

1 个答案:

答案 0 :(得分:0)

行:

$cf_name = Trim(stripslashes($_POST['Name'])); 
$cf_email = Trim(stripslashes($_POST['Email'])); 
$cf_message = Trim(stripslashes($_POST['Message'])); 

应改为

$cf_name = Trim(stripslashes($_POST['cf_name'])); 
$cf_email = Trim(stripslashes($_POST['cf_email'])); 
$cf_message = Trim(stripslashes($_POST['cf_message'])); 

变量名称无关紧要,但$_POST[''] 中的文字应与表单元素的名称值相同! 您在HTML中使用cf_作为前缀,但在PHP中没有这样做!

编辑:哎呀,我错过了什么! 您正在使用contactengine.php链接关联<a>! 您的浏览器会将此视为常规GET请求,并且不会随之发送数据! 即使您使用JavaScript代码提交表单,浏览器的默认操作(转到链接)也会超出您的代码

要解决此问题,

  1. <a>中的最后一个contacts.html标记更改为<input type="submit" value="Submit" />,将链接转换为提交按钮
  2. ; return false;添加到onclick处理程序的末尾以获取提交链接。这会阻止浏览器执行其默认行为,并提交表单。