如何将表单复选框提交到电子邮件地址?

时间:2013-08-22 16:59:23

标签: php html forms email

我创建了一个带有php脚本的html文件。 php脚本会将html中的表单发送到我的电子邮件地址。 我测试了它,一切都很完美,但只有复选框。 php脚本只向我发送一个选中复选框的选项。 怎么解决这个问题?请提前帮助并表示感谢。

我的HTML文件

<html>
<head>
<style type=text/css>
.inputtext { width: 300px; height:30px; }
<!--
.button {
display:block;
font-weight:bold;
font-size:18px;
font-family:Calibri;
color:#009933;
background-color:#ffffff;
width:150px;
text-align:center;
padding:4px;
border-top: 1px solid #b2b2b2;
border-bottom: 1px solid #b2b2b2;
border-left: 1px solid #b2b2b2;
border-right: 1px solid #b2b2b2;

}
-->
</style></head>

<form action="mail.php" method="POST">
<div align=center>
<Table border=0><tr><td>
<table border=0>

<tr>
<td valign=top><h3><font face=calibri color=#009933>Name<font face=calibri color=red>*</font></h3></td>
<td valign=top><input type="text" name="name" class=inputtext></td>
</tr>

<tr>
<td valign=top><font face=calibri color=#009933><h3>Email address<font face=calibri color=red>*</font></h3></td> 
<td valign=top><input type="text" name="email"class=inputtext></td>
</tr>

<tr>
<td valign=top><font face=calibri color=#009933><h3>Select your gender<font face=calibri color=red>*</font></h3></font></td>
<td valign=top><font face=calibri><input type="radio" name="gender" value="Male"> Male&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type=radio name=gender value=Female> Female</font></td>
</tr>

<tr>
<td valign=top><font face=calibri color=#009933><h3>Select your age<font face=calibri color=red>*</font></h3></font></td>
<td valign=top><select name="age" size=1>
                 <option value=Below 18>Below 18</option>
                 <option value=18>18</option>
                 <option value=19>19</option>

                 <option value=20>20</option>
                 <option value=21>21</option>
                 <option value=22>22</option>
                 <option value=23>23</option>
                 <option value=24>24</option>
                 <option value=25>25</option>
                 <option value=26>26</option>
                 <option value=27>27</option>
                 <option value=28>28</option>
                 <option value=29>29</option>

                 <option value=30>30</option>
                 <option value=31>31</option>
                 <option value=32>32</option>
                 <option value=33>33</option>
                 <option value=34>34</option>
                 <option value=35>35</option>
                 <option value=36>36</option>
                 <option value=37>37</option>
                 <option value=38>38</option>
                 <option value=39>39</option>

                 <option value=40>40</option>
                 <option value=41>41</option>
                 <option value=42>42</option>
                 <option value=43>43</option>
                 <option value=44>44</option>
                 <option value=45>45</option>
                 <option value=46>46</option>
                 <option value=47>47</option>
                 <option value=48>48</option>
                 <option value=49>49</option>

                 <option value=50>50</option>
                 <option value=51>51</option>
                 <option value=52>52</option>
                 <option value=53>53</option>
                 <option value=54>54</option>
                 <option value=55>55</option>
                 <option value=56>56</option>
                 <option value=57>57</option>
                 <option value=58>58</option>
                 <option value=59>59</option>

                 <option value=60>60</option>
                 <option value=61>61</option>
                 <option value=62>62</option>
                 <option value=63>63</option>
                 <option value=64>64</option>
                 <option value=65>65</option>
                 <option value=66>66</option>
                 <option value=67>67</option>
                 <option value=68>68</option>
                 <option value=69>69</option>

                 <option value=70>70</option>
                 <option value=71>71</option>
                 <option value=72>72</option>
                 <option value=73>73</option>
                 <option value=74>74</option>
                 <option value=75>75</option>
                 <option value=76>76</option>
                 <option value=77>77</option>
                 <option value=78>78</option>
                 <option value=79>79</option>

                 <option value=80>80</option>
                 <option value=81>81</option>
                 <option value=82>82</option>
                 <option value=83>83</option>
                 <option value=84>84</option>
                 <option value=85>85</option>
                 <option value=86>86</option>
                 <option value=87>87</option>
                 <option value=88>88</option>
                 <option value=89>89</option>

                 <option value=90>90</option>
                 <option value=91>91</option>
                 <option value=92>92</option>
                 <option value=93>93</option>
                 <option value=94>94</option>
                 <option value=95>95</option>
                 <option value=96>96</option>
                 <option value=97>97</option>
                 <option value=98>98</option>
                 <option value=99>99</option>
</select></td>
</tr>
</table>
<br><br>
<table border=0>

<tr><td valign=top>
<font face=calibri color=#009933><h3>1) From where did you hear about Angrybullet2 ?</h3></font>
<font face=calibri color=black><input type=radio name=hear-about value=Search engines (google, yahoo, bing)> Search engines (google, yahoo, bing, &#8230 )<br>
 <input type=radio name=hear-about value=From a friend> From a friend<br>
 <input type=radio name=hear-about value=From YouTube> From YouTube<br>
 <input type=radio name=hear-about value=Other:> Other</font>

<br><br>

<font face=calibri color=#009933><h3>2) Are you subscribed to Angrybullet2 ?</h3></font>
<font face=calibri color=black>
    <input type=radio name=sub-or-not value=Yes> Yes, I am<br>
    <input type=radio name=sub-or-not value=No,but I wish to subscribe> No, but I wish to subscribe<br>
    <input type=radio name=sub-or-not value=No,I am not, and I won't subscribe> No, I am not, and I won't subscribe<br>
    <input type=radio name=sub-or-not value=No account> I don't have a YouTube account</font>

<br><br>

<font face=calibri color=#009933><h3>3) How many videos of Angrybullet2 did you watch ?</h3></font>
<font face=calibri color=black><input type=radio name=n videos watched value=None> None<br>
                                     <input type=radio name=n-vids-watched value=1-2> 1-2<br>
                                     <input type=radio name=n-vids-watched value=3-4> 3-4<br>
                                     <input type=radio name=n-vids-watched value=5-9> 5-9<br>
                                     <input type=radio name=n-vids-watched value=10 and more> 10 and more</font>

<br><br>

<font face=calibri color=#009933><h3>4) Did you enjoy watching videos from Angrybullet2 ?</h3></font>
<font face=calibri color=black>
                     <input type=radio name=enjoy-or-not value=Yes, I did> Yes, I did<br>
                     <input type=radio name=enjoy-or-not value=No, I didn't> No, I didn't<br>
                     <input type=radio name=enjoy-or-not value=I watched none of your videos> I watched none of your videos</font>

<br><br>

<font face=calibri color=#009933><h3>5) What was the type of the video(s) you watched ?</h3></font>
<font face=calibri color=black>
                     <input type=checkbox name=vid-type value=Tutorial(s)> Tutorial(s) (how to)<br>
                     <input type=checkbox name=vid-type value=Sound effect(s)> Sound effect(s)<br>
                     <input type=checkbox name=vid-type value=Experiment(s)> Experiment(s)<br>
                     <input type=checkbox name=vid-type value=I watched none of your videos> I watched none of your videos</font>

<br><br>

<font face=calibri color=#009933><h3>6) Do you have any suggestion in order to make Angrybullet2 better ?</h3></font>
    <textarea rows="10" cols="75" name="suggestions"></textarea></td></tr></table>

<table border=0><tr><td valign=top>
<input type="submit" value="Submit survey" class=button></td><td><input type="reset" value="Reset survey fields" class=button></td></tr></table>
</td></tr></Table>
</form>
</div>
</html>

我的PHP脚本

    <?php
$name = $_POST['name'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$age = $_POST['age'];
$hear_about = $_POST['hear-about'];
$sub_or_not = $_POST['sub-or-not'];
$n_vids_watched = $_POST['n-vids-watched'];
$enjoy_or_not = $_POST['enjoy-or-not'];
$vid_type = $_POST['vid-type'];
$suggestions = $_POST['suggestions'];
$formcontent=" Name: $name \n Email: $email \n Gender: $gender \n Age: $age \n Hear_about: $hear_about \n Sub_or_not: $sub_or_not \n N_vids_watched: $n_vids_watched \n Enjoy_or_not: $enjoy_or_not \n Vid_type: $vid_type \n Suggestions: $suggestions";
$recipient = "angrybullet2@gmail.com";
$subject = "Survey response";
$mailheader = "From: $name \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! your answers are being proceeded";
?>

1 个答案:

答案 0 :(得分:1)

这是因为您忘记在每个“复选框”的名称旁边添加括号。试试这个。

 <input type="checkbox" name="vid-type[]" value="Tutorial(s)"> Tutorial(s) (how to)<br>
 <input type="checkbox" name="vid-type[]" value="Sound effect(s)"> Sound effect(s)<br>
 <input type="checkbox" name="vid-type[]" value="Experiment(s)"> Experiment(s)<br>
 <input type="checkbox" name="vid-type[]" value="I watched none of your videos"> I watched none of your videos

当您收到php中的表单时,$_POST_['vid-type']将返回一个标有值的数组。

祝你好运