Web表单发送电子邮件php错误

时间:2013-12-08 21:32:06

标签: php html webforms

我一直在使用php尝试将用户输入的内容从网络表单发送到电子邮件地址。一直乱搞PHP代码无济于事。这是网络表单:

<form action="form2email.php" method="post">
<div id="problem" class="fluid">
    <textarea name="problem" cols="50" rows="4" placeholder="enter problem here" row="4"></textarea>   
</div>
<div id="email" class="fluid">
    <input type="email" name="email" placeholder="email optional">
</div>
<div id="userOptions" class="fluid">     
        <label for="yes">Yes</label>
        <input name="publish" type="radio" value="Yes">
        <label for="no">No</label>
        <input type="radio" name="publish" value="No">    
        <label for="responder"><b>Michael</b></label>
        <input name="responder" type="checkbox" value="Michael" id="responder">
        <label for="responder"><b>Jennifer</b></label>
        <input type="checkbox" name="responder" value="Jennifer" id="responder">
        <label for="responder"><b>Mei</b></label>
        <input type="checkbox" name="responder" value="Mei" id="responder">
        <label for="responder"><b>Random</b></label>
        <input type="checkbox" name="responder" value="Random" id="responder">
</div>
<div id="submit" class="fluid">
    <input type="submit" value="Submit">
</div>
</form>

这是php:

<?php
    $emailBody= 'problem: '.$_POST['problem']."\n".
    'email: '.$_POST['email']."\n".
    'publish: '.$_POST['publish']."\n".
    'responder: '.$_POST['responder']."\n"
if(isset($_POST['submit'])) {
    mail('testing@gmail.com', 'problem' , $emailBody);
    header('location: thankyoupage.HTML');
}
else{
    header('location: testing.html');
}
?>

当我乱用代码时,问题会有所不同,但是变量之后的所有内容总是存在问题。不生成错误消息的唯一方法是在if else语句的末尾加上变量。

在上面显示的这种情况下,表单根本不起作用,它只是带我到一个页面,上面写着“找不到对象错误404”。在php文件中,它说有一个unexpected T_if,但是如果我在变量的末尾添加一个分号,则脚本根本不起作用。非常感谢帮助,并提前感谢您。

1 个答案:

答案 0 :(得分:1)

您的表单和处理程序存在2个问题。(N.B。:稍微查看我的编辑)。

1)表单:缺少name提交按钮<input type="submit" value="Submit">

如果没有命名,您的处理程序将永远不会执行mail()函数。

2)PHP处理程序:'responder: '.$_POST['responder']."\n"

末尾缺少分号

经过测试并为我工作

HTML表格

<form action="form2email.php" method="post">
<div id="problem" class="fluid">
    <textarea name="problem" cols="50" rows="4" placeholder="enter problem here" row="4"></textarea>   
</div>
<div id="email" class="fluid">
    <input type="email" name="email" placeholder="email optional">
</div>
<div id="userOptions" class="fluid">     
        <label for="yes">Yes</label>
        <input name="publish" type="radio" value="Yes">
        <label for="no">No</label>
        <input type="radio" name="publish" value="No">    
        <label for="responder"><b>Michael</b></label>
        <input name="responder" type="checkbox" value="Michael" id="responder">
        <label for="responder"><b>Jennifer</b></label>
        <input type="checkbox" name="responder" value="Jennifer" id="responder">
        <label for="responder"><b>Mei</b></label>
        <input type="checkbox" name="responder" value="Mei" id="responder">
        <label for="responder"><b>Random</b></label>
        <input type="checkbox" name="responder" value="Random" id="responder">
</div>
<div id="submit" class="fluid">
    <input type="submit" value="Submit" name="submit">
</div>
</form>

PHP处理程序

<?php
    $emailBody= 'problem: '.$_POST['problem']."\n".
    'email: '.$_POST['email']."\n".
    'publish: '.$_POST['publish']."\n".
    'responder: '.$_POST['responder']."\n";
if(isset($_POST['submit'])) {
    mail('testing@gmail.com', 'problem' , $emailBody);
    header('location: thankyoupage.HTML');
}
else{
    header('location: testing.html');
}

?>

修改

我注意到的另一件事是你有多个选项用作复选框。

还需要添加一些内容才能使其正常工作并在电子邮件中显示每个复选框。

1)在PHP处理程序中使用foreach

foreach ($_POST['responder'] as $value) {
    $check_msg .= "Checked: $value\n";
}

2)添加括号以将每个复选框视为数组:

即:name="responder[]"

新处理程序:

<?php

foreach ($_POST['responder'] as $value) {
    $check_msg .= "Checked: $value\n";
}

    $emailBody= 'problem: '.$_POST['problem']."\n".
    'email: '.$_POST['email']."\n".
    'publish: '.$_POST['publish']."\n".

 '' . $check_msg . "\n";

if(isset($_POST['submit'])) {
    mail('testing@gmail.com', 'problem' , $emailBody);
    header('location: thankyoupage.HTML');
}
else{
    header('location: testing.html');
}
?>

新表单:

<form action="form2email.php" method="post">
<div id="problem" class="fluid">
    <textarea name="problem" cols="50" rows="4" placeholder="enter problem here" row="4"></textarea>   
</div>
<div id="email" class="fluid">
    <input type="email" name="email" placeholder="email optional">
</div>
<div id="userOptions" class="fluid">     
        <label for="yes">Yes</label>
        <input name="publish" type="radio" value="Yes">
        <label for="no">No</label>
        <input type="radio" name="publish" value="No">    
        <label for="responder"><b>Michael</b></label>
        <input name="responder[]" type="checkbox" value="Michael" id="responder">
        <label for="responder"><b>Jennifer</b></label>
        <input type="checkbox" name="responder[]" value="Jennifer" id="responder">
        <label for="responder"><b>Mei</b></label>
        <input type="checkbox" name="responder[]" value="Mei" id="responder">
        <label for="responder"><b>Random</b></label>
        <input type="checkbox" name="responder[]" value="Random" id="responder">
</div>
<div id="submit" class="fluid">
    <input type="submit" value="Submit" name="submit">
</div>
</form>

从表单中选择3个名称将产生类似于以下内容的内容:

检查:Jennifer
检查:梅
选中:随机