2013-07-08 39 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

快樂編碼!