我正在尝试让我的电子邮件表单正常工作,但事实并非如此。我发现了一个准备好的fromtoemail.php
文件,我用我的变量替换了变量。我尝试了一些我发现的在线代码,但每个人都给了我同样的错误。我得到的错误是它跟踪它的第一个字段发现它为null并且我从我的代码中获取错误消息而不是遍历所有循环。我在第1行看到错误,如果是
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>
答案 0 :(得分: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]
答案 1 :(得分: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);
}