2013-07-08 40 views
1

我对HTML和CSS有相当的基本知识,从头开始构建我的网站。 我在iFrame中使用JotForm作为我的联系表单,但希望将其更改为PHP表单,所以我不依赖这些人员,并且我对表单的外观有更多的控制权。PHP联系表格的麻烦

我也初步具备形式发送一封电子邮件,空白字段 - 我试图解决这个问题,现在已完全搞砸了。

任何帮助,非常感谢。

这里是我的这是载于表格形式HTML;

<table width="400" border="0" align="left" cellpadding="5" id="form"> 

    <tr> 
<th width="134" scope="row" align="right">NAME</th> 
<th width="240" scope="row"> 
<form name="name" method="post" action="contact.php"> 
    <span id="sprytextfield1"> 
    <input type="text" name="name" id="name" value="<?php print "$nameField"; ?>"> 
    <span class="textfieldRequiredMsg">A value is required.</span><spanclass="textfieldInvalidFormatMsg">Invalid format.</span></span> 
</form></th> 
</tr> 

<tr> 
<th scope="row">&nbsp;</th> 
<th scope="row">&nbsp;</th> 
</tr> 

<tr> 
<th scope="row" align="right">EMAIL</th> 
<th scope="row"> 
<form name="email" method="post" action="contact.php"> 
    <span id="sprytextfield2"> 
    <input type="text" name="email" id="email" value="<?php print "$emailField"; ?>"> 
    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span> 
</form></th> 
</tr> 

<tr> 
<th scope="row">&nbsp;</th> 
<th scope="row">&nbsp;</th> 
</tr> 
<tr> 
<th height="128" scope="row" align="right">MESSAGE</th> 
<th scope="row"> 
<form name="message" method="post" action="contact.php"> 
    <span id="sprytextarea1"> 
    <textarea name="message" id="message" cols="45" rows="5"><?php print "$messageField"; ?></textarea> 
    <span class="textareaRequiredMsg"><br> 
    A value is required.</span></span> 
</form></th> 
</tr> 
<tr> 
<th scope="row">&nbsp;</th> 
<th scope="row">&nbsp;</th> 
</tr> 
<tr> 
<th scope="row">&nbsp;</th> 
<th scope="row"><form name="submit" method="post" action="contact.php"> 
    <input type="hidden" name="parse_var" id="parse_var" value="contactform"> 
    <input type="submit" name="submit" id="submit" value="SEND MESSAGE"> 
</form></th> 
</tr> 
<tr> 
<th scope="row">&nbsp;</th> 
<th scope="row"><?php print "$sent"; ?></th> 
</tr> 
</table> 

和我的PHP:

<?php 

     if ($_POST['parse_var'] == "form") { 

    $emailTitle = 'New Contact Form Message'; 
    $yourEmail = '[email protected]'; 

    $emailField = $_POST['email']; 
    $nameField = $_POST['name']; 
    $messageField = $_POST['message']; 

    $body = <<<EOD 
    <br><hr><br> 
    Email: $emailField <br /> 
    Name: $nameField <br /> 
    Message: $messageField <br /> 

    EOD; 

    $headers = "From: $emailField\r\n"; 
    $headers .= "content-type: text/html\r\n"; 
    $success = mail("$yourEmail", "$emailTitle", "$body", "$headers"); 

    $sent = "thankyou, your message has been successfully sent x"; 
    } 

    ?> 
+0

请不要使用,许多表,就这样90 ... – jycr753

+0

只是删除了'“' '邮件($ yourEmail,$ emailTitle,$机构,$头);' 你逝去的变量。 – jycr753

+1

而且,编辑后的代码 - 主要问题,从我所能看到的 - 多个表单标签...你应该有一个,正确地打开和关闭。+ html中的parse_var被命名为'contactform',而不是'form'。 – sinisake

回答

0

好吧,这里是功能性的,不见好形式,每场应该验证/检查(必填字段,字段格式,等等,等等... )

但是,这会工作。我已经清理了你的HTML代码,请看你在哪里犯错误...

如上所述,你的HTML结构是主要问题 - 你有多个FORM标签 - 每个表单字段 - 这是不正确的方式请参阅HTML的基本示例PHP的形式...

代码:

<?php 

    if($_POST['parse_var']=='contactform') { 

    $emailTitle = 'New Contact Form Message'; 
    $yourEmail = '[email protected]'; 


    $emailField = $_POST['email']; 
    $nameField = $_POST['name']; 
    $messageField = $_POST['message']; 



    $body = " 
    <br><hr><br> 
    Email: $emailField <br /> 
    Name: $nameField <br /> 
    Message: $messageField <br /> 

"; 

    $headers = "From: $emailField\r\n"; 
    $headers .= "content-type: text/html\r\n"; 
    $success = mail("$yourEmail", "$emailTitle", "$body", "$headers"); 

    $sent = "thankyou, your message has been successfully sent x"; 

    } 


    ?> 

<table width="400" border="0" align="left" cellpadding="5" id="form"> 

    <tr> 
<th width="134" scope="row" align="right">NAME</th> 
<th width="240" scope="row"> 
<form name="name" method="post" action="contact.php"> 
    <span id="sprytextfield1"> 
    <input type="text" name="name" id="name" value=""> 
    <span class="textfieldRequiredMsg">A value is required.</span></span> 
</th> 
</tr> 

<tr> 
<th scope="row">&nbsp;</th> 
<th scope="row">&nbsp;</th> 
</tr> 

<tr> 
<th scope="row" align="right">EMAIL</th> 
<th scope="row"> 

    <span id="sprytextfield2"> 
    <input type="text" name="email" id="email" value=""> 
    <span class="textfieldRequiredMsg">A value is required.</span></span> 
</th> 
</tr> 

<tr> 
<th scope="row">&nbsp;</th> 
<th scope="row">&nbsp;</th> 
</tr> 
<tr> 
<th height="128" scope="row" align="right">MESSAGE</th> 
<th scope="row"> 
    <span id="sprytextarea1"> 
    <textarea name="message" id="message" cols="45" rows="5"></textarea> 
    <span class="textareaRequiredMsg"><br> 
    A value is required.</span></span> 
</th> 
</tr> 
<tr> 
<th scope="row">&nbsp;</th> 
<th scope="row">&nbsp;</th> 
</tr> 
<tr> 
<th scope="row">&nbsp;</th> 
<th scope="row"> 
    <input type="hidden" name="parse_var" id="parse_var" value="contactform"> 
    <input type="submit" name="submit" id="submit" value="SEND MESSAGE"> 
</form></th> 
</tr> 
<tr> 
<th scope="row">&nbsp;</th> 
<th scope="row"><?php print "$sent"; ?></th> 
</tr> 
</table> 

PS需要您自担风险使用它。 :)为了避免垃圾邮件,发送空字段必须经过验证 - 有大量关于此的教程,请检查其中的一些。

+0

谢谢这么多!我将对此有一个很好的阅读并从我的错误中吸取教训 - 我也会研究经过验证的表格。 **谢谢** –

0

首先用<div><p>更换表重构你的HTML。表格是表格,但表格不应用于一般布局。

而且尽量避免PHP脚本里面的html。改用单独的视图。 PHP框架可能会帮助你。看看Codeginiter

认为<form>标签作为一个div容器。用它包裹整个表格。因此,从<form>开始,然后将所有表单相关元素放在里面,最后用</form>关闭。这使得html文档中的表单部分脱颖而出,因此您可以更轻松地使用(可读性)。

我已经改变了你的代码示例:

形式HTML:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Form Page</title> 
    <!-- Your header items like meta and link for css stylesheets --> 
</head> 
<body> 

    <div id="container"> 
     <form id="form-id" action="contact.php" method="POST"> 
      <input id="parse_var" type="hidden" name="parse_var" value="contactform"> 

      <div> 
       <label for="name">Name</label> 
       <input id="name" type="text" name="name" value="<?php echo $nameField; ?>"> 
       <?php if (empty($nameField)) echo '<span class="textfield requiredmsg">A value is required.</span>'; ?> 
       <?php if (empty($nameField)) echo '<span class="textfield invalidformatmsg">Invalid format.</span>'; ?> 
      </div> 

      <div> 
       <label for="email">E-Mail</label> 
       <input id="email" type="text" name="email" value="<?php echo $emailField; ?>"> 
       <?php if (empty($emailField)) echo '<span class="textfield requiredmsg">A value is required.</span>'; ?> 
       <?php if (empty($emailField)) echo '<span class="textfield invalidformatmsg">Invalid format.</span>'; ?> 
      </div> 

      <div> 
       <label for="message">Message</label> 
       <textarea id="message" name="message"><?php echo $messageField; ?></textarea> 
       <?php if (empty($messageField)) echo '<span class="textfield requiredmsg">A value is required.</span>'; ?> 
      </div> 

      <div class="form-controls"> 
       <input type="submit" name="submit" id="submit" value="Send message"> 
      </div> 
     </form> 
    </div> 

</body> 
</html> 


关于你的PHP代码,检查了这一点:

<?php 

// Create a view load function 
function loadView($view) 
{ 
    // Change this to where you save the view files 
    $viewPath = '/'.$view.'.php'; 

    // Check if the requested view exists 
    if (! file_exists($viewPath)) return false; 

    // This will put the view source in a variable 
    // without flushing to the screen (like echo/print) 
    ob_start(); 
    include $viewPath; 
    $buffer = ob_get_contents(); 
    @ob_end_clean(); 

    return $buffer; 
} 

// Check if the form was submitted 
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['parse_var'])) 
{ 
    // Differentiate between forms 
    if ($_POST['parse_var'] == 'contactform') 
    { 
     // Check if all required data was submitted 
     if (isset($_POST['name'], $_POST['email'], $_POST['message']) 
      && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
     { 
      // Mail Message Settings 
      $emailTitle = 'New Contact Form Message'; 
      $yourEmail = '[email protected]'; 

      // Get the form data (remember to secure user input against e.g. CSRF) 
      $emailField = $_POST['email']; 
      $nameField = htmlentities($_POST['name']); 
      $messageField = strip_tags($_POST['message']); 

      // Load the email body view. The variables above 
      // will be accessible in the view. 
      $body = loadView('email_body'); 

      // Set the mail headers 
      $headers = "From: $emailField\r\n"; 
      $headers .= "Content-type: text/html\r\n"; 

      // Send the message - I highly recommend you to use something like SwiftMailer! 
      $mail = mail($yourEmail, $emailTitle, $body, $headers); 

      // Check if the mail was sent 
      if ($mail) 
      { 
       echo 'Sweet! Your message has been successfully sent.'; 
      } 
      else 
      { 
       echo 'Whoops! The message was not sent.'; 
      } 
     } 
     else 
     { 
      // Here you could return an error message 
     } 
    } 
} 


请注意,我将双引号"更改为单引号'。为什么?看到这个SO问题:https://stackoverflow.com/a/3446286/2493918

我强烈建议您使用类似SwiftMailer的东西来发送带有PHP的电子邮件!使用本地mail()函数可能并不总是按预期工作,并且大多数已发送的邮件将被抛入收件人SPAM文件夹(如Gmail等)。

通读代码评论以了解更多关于我所做的更改。

一个很好的资源来了解更多PHP提供的是PHP文档,可以在这里找到:例如ob_start()函数http://www.php.net/manual/en/function.ob-start.php

快乐编码!