2012-08-28 148 views
0

我试图创建的表单有问题。基本上,即使PHP代码是正确的,它也不允许我将电子邮件发送给收件人。这可能是TRIM PHP代码的问题吗?PHP表单提交

<?php 
    if ($_POST['submit']) { 
     if (empty($_Post['name']) || 
      empty($_POST['email']) || 
      empty($_POST['comments'])) { 

      $error = true; 
     } 
     else { 

      $to = "[email protected]"; 

      $name = trim($_POST['name']); 
      $email = trim($_POST['email']); 
      $comments = trim($_POST['comments']); 

      $subject = "Contact Form"; 

      $messages = "Name: $name \r\n Email: $email \r\n Comments: $comments"; 
      $headers = "From:" . $name; 
      $mailsent = mail($to, $subject, $message, $headers); 

      if ($mailsent) { 
       $sent = true; 
      } 
     } 
    } 
?> 

我的HTML是:

<?php if($error == true){ ?> 

    <p class="error">Text</p> 

<?php } if($sent == true) { ?> 

    <p class="sent">Text</p> 

<?php } ?> 

<div id="form"> 
    <form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
     <fieldset> 
      <h4>Contact Me!</h4> 
      <label for="name">Name:</label> 
       <input type="text" name="name" id="name"/> 
       <label for="email"/>Email:</label> 
       <input type="text" name="email" id="email"/> 
       <label for="comments" id="comments">Comments:</label> 
       <textarea name="comments" id=""></textarea> 
       <fieldset> 
        <input class="btn" type="submit" name="submit" class="submit" value="Send email"/> 
        <input class="btn" type="reset" value="Reset"/> 
       </fieldset> 
     </fieldset> 
    </form> 
+1

尝试后有什么$ mailsent的价值,发送?尝试var_dump($ mailsent)并查看结果是什么。另外,你确定邮件没有进入垃圾文件夹吗? –

+1

当您尝试提交时,也应该有一些php错误消息。粘贴在这里。 –

+1

您已经创建了一个名为'$ messages'的变量,但是它将'$ message'传递给邮件函数。这是它在你的代码中是如何的呢,还是只在这个问题上有一个错字? –

回答

2

试试这个(我不得不修复了一些东西):

<?php 
    $error = false; 
    $sent = false; 

    if(isset($_POST['submit'])) { 
     if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) { 
      $error = true; 
     } 
     else { 
      $to = "[email protected]"; 

      $name = trim($_POST['name']); 
      $email = trim($_POST['email']); 
      $comments = trim($_POST['comments']); 

      $subject = "Contact Form"; 

      $message = "Name: $name \r\n Email: $email \r\n Comments: $comments"; 
      $headers = "From:" . $name; 
      $mailsent = mail($to, $subject, $message, $headers); 

      if($mailsent) { 
       $sent = true; 
      } 
     } 
    } 
?> 

<?php if($error == true){ ?> 
<p class="error">Text</p> 
<?php } if($sent == true) { ?> 
<p class="sent">Text</p> 
<?php } ?> 
<div id="form"> 
    <form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
     <fieldset> 
      <h4>Contact Me!</h4> 
      <label for="name">Name:</label> 
       <input type="text" name="name" id="name"/> 
       <label for="email"/>Email:</label> 
       <input type="text" name="email" id="email"/> 
       <label for="comments" id="comments">Comments:</label> 
       <textarea name="comments" id=""></textarea> 
       <fieldset> 
       <input class="btn" type="submit" name="submit" class="submit" value="Send email"/> 
       <input class="btn" type="reset" value="Reset"/> 
       </fieldset> 
     </fieldset> 
    </form> 
</div> 
+0

感谢你的努力 –

1
<script> 
    window.scroll(0,0); 
    $('#table_content').dataTable({ 
     "bJQueryUI": true, 
     "bProcessing": true, 
     "sPaginationType": "full_numbers", 
     "sAjaxSource": "../gym_facility_v0/load_facility.php" 
    }); 

    $("#men a").click(function(){ 
     var link = $(this); 

     $.ajax({ url: "../"+link.attr("href"), 
      dataType: 'html', 
      data: {post_loader: 1}, 
      success: function(data){ 
       $("#content").html(data); 
      }}); 
      return false; 
    }); 
</script> 

<div class="title"><h5> Gym Facility</h5></div> 

<div class="table"> 
    <div class="head" id="men"><h5 class="iAdd"><a class="open-add-client-dialog" href="gym_facility_v0/form_facility.php"><i class="icon-plus"></i>Add Facility</a></h5></div> 
     <div class="dataTables_wrapper" id="example_wrapper"> 
     <div class=""> 
     <div class="dataTables_filter" id="example_filter"> 

      <!--<label>Search: <input type="text" placeholder="type here..."> 
      <div class="srch"> 
      </div> 
      </label>--> 

     </div> 
    </div> 
    <table cellpadding="0" cellspacing="0" border="0" class="display" id="table_content"> 
     <thead> 
      <tr> 
      <th class="ui-state-default" rowspan="1" colspan="1" style="width: 2%;"> 
      <div class="DataTables_sort_wrapper">S.No 
      </div></th> 
      <th class="ui-state-default" rowspan="1" colspan="1" style="width: 227px;"> 
      <div class="DataTables_sort_wrapper">Gym Facility</div></th><th class="ui-state-default" rowspan="1" colspan="1" style="width: 130px;"> 
      <div class="DataTables_sort_wrapper"> Action</div></th></tr> 
     </thead> 

     <tbody><tr class="gradeA odd"> 
        <td colspan="5" class="gradeA">Loading data from server</td> 
       </tr> 
     </tbody> 
    </table> 

</div><!-- End of .content --> 
+4

如果你能详细说明你的代码的作用,为什么它对问题有帮助以及应该如何实现,它可能会对问题提供帮助。 – Jens

+2

请用一些额外的描述来改进您的答案,您的当前答案可能对未来的读者不清楚 –