0

我正在尝试使我的电子邮件表单正常工作,但它没有。我找到了一个现成的fromtoemail.php文件,并用我的替换了变量。我尝试了一些我发现的在线代码,但每个人都给了我同样的错误。我收到错误,它跟踪的第一个字段发现它为空,我从我的代码中收到错误消息,而不是在所有循环中移动。我在第一行用 if 得到错误

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

这是来自 php 和来自我的 html 的代码。

PHP:

<?php 
 $to = $_REQUEST['myemail@gmail.com'] ; 
 $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 webmaster@YourCompany.com"; } 
 }
 }
 }
 ?> 

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>
4

2 回答 2

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]
于 2012-12-24T16:05:37.023 回答
0

这是我的教学示例,展示了表单到电子邮件脚本的基本部分。希望它可以帮助您解决问题。最好的,~雷

<?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  = "NoReply@Your.org";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS - CHANGE THESE FOR YOUR WORK
$to[]  = "You@Your.org";
$to[]  = "Her@Your.org";
$to[]  = "Him@Your.org";


// 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);
}
于 2012-12-24T16:11:22.407 回答