2012-12-24 45 views
0

我想讓我的電子郵件表格工作,但它沒有。我發現一個準備好的fromtoemail.php文件,我用我的替換變量。我嘗試了一些我發現的在線代碼,但每個人都給我提供了同樣的錯誤。我得到錯誤,我的第一個字段,它跟蹤它發現它爲空,我從我的代碼中獲取錯誤消息,而不是全部遍歷所有的循環。我在這裏得到了錯誤的一號線與是否是在沒有打開電子郵件客戶端的PHP中的電子郵件表格

if($author == '') {print "You have not entered an author, please go back and try again";} 

下面是從PHP代碼和HTML我。

PHP:

<?php 
$to = $_REQUEST['[email protected]'] ; 
$author = $_REQUEST['author'] ; 
$email = $_REQUEST['email'] ; 
$subject = $_REQUEST['subject'] ; 
$text = $_REQUEST['text'] ; 
$headers = "From: $from"; 
$subject = "$subject"; 

$fields = array(); 
$fields{"author"} = "author"; 
$fields{"email"} = "email"; 
$fields{"subject"} = "subject"; 
$fields{"text"} = "text"; 

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 

$headers2 = "$email"; 
$subject2 = "Thank you for contacting us"; 
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com"; 

if($author == '') {print "You have not entered an author, please go back and try again";} 
else { 
if($email == '') {print "You have not entered a email, please go back and try again";} 
else { 
if($subject == '') {print "You have not entered a subject, please go back and try again";} 
else { 
$send = mail($to, $subject, $body, $headers); 
$send2 = mail($email, $subject2, $autoreply, $headers2); 
if($submit) 
{header("Location: http://www.sofisarti.com/index-english.html");} 
else 
{print "We encountered an error sending your mail, please notify [email protected]"; } 
} 
} 
} 
?> 

HTML:

<form action="formtoemail.php" method="post" enctype="text/plain"> 
          <p> 
           <label for="author">Name:</label> 
           <input type="text" id="author" name="author" class="required input_field" /> 
          </p> 
          <p> 
           <label for="email">Email:</label> 
           <input type="text" id="email" name="email" class="validate-email required input_field" /> 
          </p> 
          <p class="no_margin_right"> 
           <label for="subject">Subject:</label> 
           <input type="text" name="subject" id="subject" class="input_field" /> 
          </p> 
          <div class="cleaner h20"></div> 

          <label for="text">Message:</label> 
          <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea> 
          <div class="cleaner h20"></div> 

          <input type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l" /> 
          <input type="reset" value="Reset" id="reset" name="reset" class="submit_btn float_r" /> 
         </form> 
+2

這是幹什麼的? $ fields {「author」} =「author」; –

+1

將'var_dump($ _ REQUEST);'放在打開的PHP標記之後併發布輸出。 –

+1

建議您將error_reporting(E_ALL)添加到腳本的頂部。可能會有一些依賴未定義的變量,這將引發關於這樣的事情的通知。 –

回答

0

我建議的形式驗證的這種風格。這應該使更清晰,您將能夠立即顯示所有錯誤消息

<?php 

$Author = $_POST['author']; 
$Message = $_POST['text']; 

$Errors = array(); 

if (strlen($Author) < 3){ 
    $Errors[] = 'Please enter your full name'; 
} 

if (strlen($Message) < 10){ 
    $Errors[] = 'Please describe your message in more detail.'; 
} 

// Add if- conditions for email, subject, etc.. 

if (!empty($Errors)){ 

    $Errorbody = '<ul>'; 
    foreach ($Errors as $Error){ 
     $Errorbody .= '<li>'.$Error.'</li>';  
    } 
    $Errorbody .= '</ul>'; 
} else { 
    // the rest of the stuff, sending email, header towards thank-you page. 
} 
?> 

[HTML HEADER ETC] 
<?php 
if (isset($Errorbody)){ 
    echo $Errorbody; 
} 
?> 

[HTML FORM HERE] 
+0

謝謝大家的回覆。我會嘗試你給我的代碼,看看我得到了什麼 – lantonis

0

這是我的教學示例,它顯示了form-to-email腳本的基本部分。希望它能幫助你理清問題。最佳,〜Ray

<?php // RAY_form_to_email.php 
error_reporting(E_ALL); 


// SEND MAIL FROM A FORM 


// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK 
$from = "[email protected]"; 
$subj = "Contact Form"; 

// THIS IS AN ARRAY OF RECIPIENTS - CHANGE THESE FOR YOUR WORK 
$to[] = "[email protected]"; 
$to[] = "[email protected]"; 
$to[] = "[email protected]"; 


// IF THE DATA HAS BEEN POSTED 
if (!empty($_POST['email'])) 
{ 
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA 
    $email  = clean_string($_POST["email"]); 
    $name  = clean_string($_POST["name"]); 
    $telephone = clean_string($_POST["telephone"]); 

    // CONSTRUCT THE MESSAGE THROUGH STRING CONCATENATION 
    $content = NULL; 
    $content .= "You have a New Query From $name" . PHP_EOL . PHP_EOL; 
    $content .= "Tel No: $telephone" . PHP_EOL; 
    $content .= "Email: $email" . PHP_EOL; 

    // SEND MAIL TO EACH RECIPIENT 
    foreach ($to as $recipient) 
    { 
     if (!mail($recipient, $subj, $content, "From: $from\r\n")) 
     { 
      echo "MAIL FAILED FOR $recipient"; 
     } 
     else 
     { 
      echo "MAIL WORKED FOR $recipient"; 
     } 
    } 

    // PRODUCE THE THANK-YOU PAGE 
    echo '<p>THANK YOU</p>' . PHP_EOL; 
} 


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT 
$form = <<<ENDFORM 
<form method="post"> 
Please enter your contact information 
<br/>Email: <input name="email" /> 
<br/>Phone: <input name="telephone" /> 
<br/>Name: <input name="name" /> 
<br/><input type="submit" /> 
</form> 
ENDFORM; 

echo $form; 


// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM 
function clean_string($str) 
{ 
    // IF MAGIC QUOTES IS ON, WE NEED TO REMOVE SLASHES 
    $str = stripslashes($str); 

    // REMOVE EXCESS WHITESPACE 
    $rgx 
    = '#'    // REGEX DELIMITER 
    . '\s'    // MATCH THE WHITESPACE CHARACTER(S) 
    . '\s+'    // MORE THAN ONE CONTIGUOUS INSTANCE OF WHITESPACE 
    . '#'    // REGEX DELIMITER 
    ; 
    $str = preg_replace($rgx, ' ', $str); 

    // REMOVE UNWANTED CHARACTERS 
    $rgx 
    = '#'    // REGEX DELIMITER 
    . '['    // START OF A CHARACTER CLASS 
    . '^'    // NEGATION - MATCH NONE OF THE CHARACTERS IN THIS CLASS 
    . 'A-Z0-9'   // KEEP LETTERS AND NUMBERS 
    . '"'    // KEEP DOUBLE QUOTES 
    . "'"    // KEEP SINGLE QUOTES 
    . '@&+:?_.,/\-'  // KEEP SOME SPECIAL CHARACTERS (ESCAPED HYPHEN) 
    . ' '    // KEEP BLANKS 
    . ']'    // END OF THE CHARACTER CLASS 
    . '#'    // REGEX DELIMITER 
    . 'i'    // CASE-INSENSITIVE 
    ; 
    $str = preg_replace($rgx, NULL, $str); 

    return trim($str); 
} 
相關問題