php电子邮件表单中的空变量

时间:2012-12-26 10:26:52

标签: php html

我在php文件中有一个简单的电子邮件表单,我尝试发送电子邮件,但似乎我的变量为空。我的代码后面有一个回声,只是为了测试我的变量里面是否有任何值而且它们没有被打印出来。唯一印刷的是'完成'和'电子邮件$ to'。我做错了吗?我从youtube上的一个帖子中跟踪了这个方法,他完成了同样的事情并且对他有用。我也尝试了更多的电子邮件php文件,但仍然没有。 这是我的HTML和PHP代码。

php代码:

 <?php 

$name = $_REQUEST['author'];
$from = $_REQUEST['email'];
$subj = $_REQUEST['sub'];
$message = $_REQUEST['msg'];

$headers .= "From: ".$from;

$to = "mygmail@gmail.com";

$subject = $subj;
$body = $message;

if(mail($to,$subject,$body,$headers)) {
echo "An e-mail was sent to ".$to." with the subject: ".$subject;
} else {
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address ".$to." is valid";
}

?>

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="sub" id="sub" class="input_field" />
                            </p>
                            <div class="cleaner h20"></div>

                            <label for="text">Message:</label> 
                            <textarea id="msg" name="msg" 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 个答案:

答案 0 :(得分:2)

您通过POST发送,并通过GET尝试$_POST而不是$_GET在PHP中检索

HTML中的OR -

将表单操作更改为GET而不是POST

答案 1 :(得分:0)

在您的HTML代码中,将“POST”方法更改为“GET”

<form action="formtoemail.php" method="get" 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="sub" id="sub" class="input_field" />
                        </p>
                        <div class="cleaner h20"></div>

                        <label for="text">Message:</label> 
                        <textarea id="msg" name="msg" 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 :(得分:0)

使用“get”方法:

<form action="formtoemail.php" method="get" enctype="text/plain">

答案 3 :(得分:0)

你需要改变这个:

$headers .= "From: $email";

对此:

$headers = "From: ".$from;

并且还改变了这个:

$subject = "$subj";
$body = "$message";

mail($to, $subject, $body, $headers);

if(mail($to, $subject, $body, $headers)) {
    echo "An e-mail was sent to $to with the subject: $subject";
} else {
    echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid";
}

对此:

$subject = $subj;
$body = $message;

if(mail($to, $subject, $body, $headers)) {
    echo "An e-mail was sent to ".$to." with the subject: ".$subject;
} else {
    echo "There was a problem sending the mail. Check your code and make sure that the e-mail address ".$to." is valid";
}