3

我想在我的表格上有一个需要 *fullname、*email 地址以及 *subject 和 *message 的联系表。我按照教程开发了我的表单,但由于某种原因它没有发送我的测试消息。

我没有足够的 PHP 经验来弄清楚我做错了什么,所以我希望得到关于如何解决这个问题的建议。欢迎提出所有问题、建议和可能的解决方案。谢谢。

联系表格 php: 代码:

<?php 

// EDIT THE FOLLOWING LINE BELOW AS REQUIRED

$send_email_to = "jb@me.com";

function send_email($name,$email,$email_subject,$email_message)
{
  global $send_email_to;  

  $headers = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
  $headers .= "From: ".$email. "\r\n";

  $message = "<strong>Email = </strong>".$email."<br>";
  $message .= "<strong>Name = </strong>".$name."<br>";
  $message .= "<strong>Message = </strong>".$email_message."<br>";
  @mail($send_email_to, $email_subject, $message,$headers);
  return true;
}

function validate($name,$email,$message,$subject)
{
  $return_array = array();
  $return_array['success'] = '1';
  $return_array['name_msg'] = '';
  $return_array['email_msg'] = '';
  $return_array['message_msg'] = '';
  $return_array['subject'] = '';

 if($email == '')
  {
    $return_array['success'] = '0';
    $return_array['email_msg'] = 'email is required';
  }
  else
  {
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email)) {
      $return_array['success'] = '0';
      $return_array['email_msg'] = 'enter valid email.';  
    }
  }

  if($name == '')
  {
    $return_array['success'] = '0';
    $return_array['name_msg'] = 'name is required';
  }
  else
  {
     $string_exp = "/^[A-Za-z .'-]+$/";
    if (!preg_match($string_exp, $name)) {
      $return_array['success'] = '0';
     $return_array['name_msg'] = 'enter valid name.';
    }
  }


  if($subject == '')
  {
    $return_array['success'] = '0';
    $return_array['subject_msg'] = 'subject is required';
  }

  if($message == '')
  {
    $return_array['success'] = '0';
    $return_array['message_msg'] = 'message is required';
  }
  else
  {
    if (strlen($message) < 2) {
      $return_array['success'] = '0';
      $return_array['message_msg'] = 'enter valid message.';
    }
  }
  return $return_array;
}

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];

$return_array = validate($name,$email,$message,$subject);
if($return_array['success'] == '1')
{
  send_email($name,$email,$subject,$message);
}

header('Content-type: text/json');
echo json_encode($return_array);
die();

?>

联系表格 HTML: 代码:

<fieldset id="contact_form">
          <div id="msgs"> </div>
          <form id="cform" name="cform" method="post" action="">
            <input type="text" id="name" name="name" value="Full Name*" onfocus="if(this.value == 'Full Name*') this.value = ''"
                            onblur="if(this.value == '') this.value = 'Full Name*'" />
            <input type="text" id="email" name="email" value="Email Address*" onfocus="if(this.value == 'Email Address*') this.value = ''"
                            onblur="if(this.value == '') this.value = 'Email Address*'" />
            <input type="text" id="subject" name="subject" value="Subject*" onfocus="if(this.value == 'Subject*') this.value = ''"
                            onblur="if(this.value == '') this.value = 'Subject*'" />
            <textarea id="msg" name="msg" onfocus="if(this.value == 'Your Message*') this.value = ''"
                            onblur="if(this.value == '') this.value = 'Your Message*'">Your Message*</textarea>
            <button id="submit" class="button"> Send Message</button>
          </form>
        </fieldset>
4

3 回答 3

0

您的表单操作未设置为任何内容。您需要将其指向您那里的发送电子邮件的脚本。

IE:

<form id.. name.. method.. action="/handle_post.php">
于 2012-09-06T21:07:46.577 回答
0

设置正确的操作,然后尝试它应该可以工作..如果它仍然不起作用,请使用 curl 实用程序测试它以查看您的脚本是否正常..我看到的另一个错误是您的文本区域表单名称是 msg 在服务器上你期待你的帖子请求它有消息。那行不通..如果您仍然遇到问题,那么我们将进一步调试:)

于 2012-09-06T21:12:56.550 回答
0

首先,您需要为一些将处理$_POST输入 ( contactform.php) 或$_SERVER['PHP_SELF']同一文件的脚本设置操作。

其次,您应该通过输入type=submit而不是按钮发送数据。

于 2012-09-06T21:14:27.427 回答