0

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

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

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

联系人.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>

联系方式.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\">";
}
?>
4

1 回答 1

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[''] 应该和表单元素的name值一样! 您在 HTML 中为名称添加了前缀cf_,但在您的 PHP 中没有这样做!

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

要解决这个问题,要么

  1. 将最后一个<a>标签更改contacts.html<input type="submit" value="Submit" />,将链接变成提交按钮,或者
  2. 添加到提交链接; return false;的处理程序的末尾。onclick这会阻止浏览器执行其默认行为,并提交表单。
于 2013-03-19T03:00:12.397 回答