我有一个简单的html表单应该使用POST方法提交表单,它将在mail.php上发送表单内容以发送电子邮件。我已使用以下代码验证了在服务器上运行的php邮件功能:
$headers = 'From: webmaster@example.com'; mail('myemail@mail.com', 'test email yo', 'This is a test email message', $headers, '-fwebmaster@example.com');
我用来发送邮件的PHP代码如下:
mail('myemail@mail.com', $_POST['name'], $_POST['email'], $_POST['phone'], $_POST['message']);
应该发送信息的html表单是:
<form id="contact-form" class="contact-form" method="post" action="mail.php">
<div class="success-message">Contact form submitted.</div>
<div class="coll-1">
<p class="mbot3">Name*</p>
<label class="name">
<input type="text" placeholder="" data-constraints="@Required @JustLetters" name="name"/>
<span class="empty-message">*This field is required.</span>
<span class="error-message">*This is not a valid name.</span>
</label>
</div>
<div class="coll-2">
<p class="mbot3">E-mail*</p>
<label class="email">
<input type="text" placeholder="" data-constraints="@Required @Email" name="email"/>
<span class="empty-message">*This field is required.</span>
<span class="error-message">*This is not a valid email.</span>
</label>
</div>
<div class="coll-3">
<p class="mbot3">Phone</p>
<label class="phone">
<input type="text" placeholder="" data-constraints="@JustNumbers" name="phone"/>
<span class="empty-message">*This field is required.</span>
<span class="error-message">*This is not a valid phone.</span>
</label>
</div>
<label class="message">
<span class="mbot3">Message*</span>
<textarea placeholder="" data-constraints="@Required @Length(min=20,max=999999)" name="message"></textarea>
<span class="empty-message">*This field is required.</span>
<span class="error-message">*The message is too short.</span>
</label>
<div>
<a href="#" data-type="submit" class="btn-link btn-link1"><img src="img/arrow1.png" alt="">submit</a>
<p class="req">* Required fields</p>
</div>
</form>
我无法在电子邮件中获取表单的内容,我可能错误地认为问题与表单有关,并且能够将信息发布到php?
此代码来自我自己未编写的预制模板。
编辑:根据建议,我添加了名称属性但是没有解决问题。更新了HTML以反映当前代码。
Edit2:已经解决了问题,我不得不删除id =“contact-form”,不知道与此相关的是什么阻止了表单提交但是这样做了。从那里我从原始形式中取出样式并将它们添加到我用它替换它的新ID中。
答案 0 :(得分:3)
您错过输入和textarea 字段的名称
<input type="text" placeholder="" data-constraints="@Required @JustLetters" name = "name"/>
名称属性应具有 $ _ POST ['name']
的相同名称答案 1 :(得分:0)
检查HTML
代码
<input type="text" placeholder="" data-constraints="@Required @JustLetters" />
<input type="text" placeholder="" data-constraints="@Required @Email" />
<input type="text" placeholder="" data-constraints="@JustNumbers"/>
<textarea placeholder="" data-constraints="@Required @Length(min=20,max=999999)"></textarea>
并使用以下代码替换它们,在提交表单后,您错过了添加用于引用表单数据的name
属性,name
属性。
<input type="text" name="name" placeholder="" data-constraints="@Required @JustLetters" />
<input type="text" name="email" placeholder="" data-constraints="@Required @Email" />
<input type="text" name="phone" placeholder="" data-constraints="@JustNumbers"/>
<textarea name="message" placeholder="" data-constraints="@Required @Length(min=20,max=999999)"></textarea>