0

我正在尝试制作一个将发送到我的电子邮件的表格。但它在提交时显示错误。未显示用于验证表单的 Javascript,但它工作正常。我不确定下面的代码有什么问题,整天都在试图弄清楚并阅读各种线程但无济于事。

下面是我处理表单的 php 代码。

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

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "me@gmail.com";
$email_subject = "Nexwave Form";


function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

$name = $_POST['name']; // required
$designation = $_POST['designation'];
$company = $_POST['company'];
$contact = $_POST['contact'];
$email = $_POST['email']; // not required
$users = $_POST['users']; // required

$error_message = "";
$string_exp = "/^[A-Za-z .'-]+$/";

$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Designation: ".clean_string($designation)."\n";
$email_message .= "company: ".clean_string($company)."\n";
$email_message .= "Contact ".clean_string($contact)."\n";
$email_message .= "email: ".clean_string($email)."\n";
$email_message .= "Number of Users: ".clean_string($users)."\n";


// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
echo (int) mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

下面是我的html代码:

<form name="form" action="send_mail.php" onSubmit="return validateForm();" method="POST">
&nbsp;Name<br/><input name="name" type="text" style="width:90%;"/><br/>
&nbsp;Designation<br/><input name="designation" type="text" style="width:90%;"/><br/>
&nbsp;Company<br/><input name="company" type="text"  style="width:90%;"/><br/>
&nbsp;Contact Number<br/><input name="contact" type="text" style="width:90%;"/><br/>
&nbsp;Email<br/><input name="email" type="text" style="width:90%;"/><br/>
&nbsp;Number of Users<br/><input name="users" type="text" style="width:90%;"/>
<span style="text-align:right;display:block;width:174px;"><input style="height:25px;margin-top:20px;margin-bottom:10px;background-color:#ffffff;border:0;color:#009110;" type="submit" class="submit" value="Submit" /></span>
</form>

以下是错误:即使在更改 email_address 和 email_from 后,我也会得到。

"; echo $error."

"; echo "Please go back and fix these errors.

"; die(); } $name = $_POST['name']; // required $designation = $_POST['designation']; $company = $_POST['company']; $contact = $_POST['contact']; $email = $_POST['email']; // not required $users = $_POST['users']; // required $error_message = ""; $string_exp = "/^[A-Za-z .'-]+$/"; $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Name: ".clean_string($name)."\n"; $email_message .= "Designation: ".clean_string($designation)."\n"; $email_message .= "company: ".clean_string($company)."\n"; $email_message .= "Contact ".clean_string($contact)."\n"; $email_message .= "email: ".clean_string($email)."\n"; $email_message .= "Number of Users: ".clean_string($users)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email."\r\n" . 'X-Mailer: PHP/' . phpversion(); echo (int) mail($email_to, $email_subject, $email_message, $headers); ?> Thank you for contacting us. We will be in touch with you very soon. 

非常感激你的帮助

4

1 回答 1

0

如上面的问题评论所述,此脚本中存在许多错误。

  • 电子邮件表单名称email_address$_POST['email']
  • $email_from在调用它之前,您没有定义。

在发布这样的问题之前,我会做一些简单的测试,并尝试一一回显所有变量,看看你得到了什么。这将向您显示提到的两个错误。如果您无法解决它并且仍然有错误,那么请发布您的错误消息,以便其他人可以提供更多帮助,而不仅仅是它不起作用。

于 2013-06-10T17:31:29.357 回答