PHP:从表单中获取电子邮件并在单击按钮时发送它
Posted
技术标签:
【中文标题】PHP:从表单中获取电子邮件并在单击按钮时发送它【英文标题】:PHP: Getting the email from a form and sending it when a button is clicked 【发布时间】:2015-01-19 10:48:37 【问题描述】:所以我希望网站向员工和客户发送 2 封电子邮件。工作人员有一个预设的电子邮件,而客户必须将其写在文本框(表格)中。我试图获取客户的邮件。
<form id='contact-info' method='post' novalidate class="form contact-info">
<div class="contact-info__field contact-info__field-mail">
<input type='email' name='user-mail' placeholder='Your email' class="form__mail">
</div>
<div class="contact-info__field contact-info__field-tel">
<input type='tel' name='user-tel' placeholder='Phone number' class="form__mail">
</div>
</form>
</div>
<div class="order">
<a href="Rezervare-final.html" name="button1" class="btn btn-md btn--warning btn--wide">Cumpara</a>
</div>
And here's the php right after the html.
<?php
if (isset($_POST['button1']))
$toclient = "Test client";
$tostaff = "Test staff";
$maile = $_POST['user-mail'];
$toclient = wordwrap($toclient, 70);
$tostaff = wordwrap($tostaff, 70);
mail($maile, 'My Subject', $toclient);
mail('example@yahoo.com', 'My Subject', $tostaff);
?>
我可以收到员工的电子邮件,但不能收到客户的电子邮件。问:为什么收不到客户邮件?
【问题讨论】:
【参考方案1】:您没有使用提交按钮提交数据,因此您无法获得$_POST['user-mail']
值。
如果你使用类似的代码,它应该可以工作
<form id='contact-info' method='post' class="form contact-info" action="" >
<div class="contact-info__field contact-info__field-mail">
<input type='email' name='user-mail' placeholder='Your email' class="form__mail">
</div>
<div class="contact-info__field contact-info__field-tel">
<input type='tel' name='user-tel' placeholder='Phone number' class="form__mail">
</div>
<input type='submit' name='button1' value="submit">
</form>
</div>
【讨论】:
【参考方案2】:除非您混合了一些不与我们共享的 javascript,否则该表单将永远不会提交; button1 是指向 Rezervare-final.html 的链接
因此,您需要将链接替换为 提交按钮:
<input value= "Cumpara" type = "submit"/>
或
<button type="submit">Cumpara</button>
在您的处理脚本中不要监听按钮;如果那是唯一的必填字段,请收听电子邮件字段:
if(isset($_POST['user-mail']))
//process form/emails
【讨论】:
【参考方案3】:试试这个.....文件名应该是 Rezervare-final.php 而不是 Rezervare-final.html
<form id='contact-info' method='post' action="Rezervare-final.php" class="form contact-info">
<div class="contact-info__field contact-info__field-mail">
<input type='email' name='user-mail' placeholder='Your email' class="form__mail">
</div>
<div class="contact-info__field contact-info__field-tel">
<input type='tel' name='user-tel' placeholder='Phone number' class="form__mail">
</div>
</form>
</div>
<div class="order">
<input type='submit' name='button1' value="Cumpara" class="btn btn-md btn--warning btn--wide>
</div>
And here's the php right after the html.
<?php
if (isset($_POST['button1']))
$toclient = "Test client";
$tostaff = "Test staff";
$maile = $_POST['user-mail'];
$toclient = wordwrap($toclient, 70);
$tostaff = wordwrap($tostaff, 70);
mail($maile, 'My Subject', $toclient);
mail('example@yahoo.com', 'My Subject', $tostaff);
?>
【讨论】:
【参考方案4】:<form id='contact-info' method='post' novalidate class="form contact-info">
<div class="contact-info__field contact-info__field-mail">
<input type='email' name='user-mail' placeholder='Your email' class="form__mail">
</div>
<div class="contact-info__field contact-info__field-tel">
<input type='tel' name='user-tel' placeholder='Phone number' class="form__mail">
<input type="submit" name="submit" class="submit">
</div>
</form>
<?php
if (isset($_POST['submit']))
$toclient = "Test client";
$tostaff = "Test staff";
$maile = $_POST['user-mail'];
$toclient = wordwrap($toclient, 70);
$tostaff = wordwrap($tostaff, 70);
mail($maile, 'My Subject', $toclient);
mail('example@yahoo.com', 'My Subject', $tostaff);
?>
试试这个,我相信你在你的 php 脚本中遗漏了一些标题,请参阅 php 手册希望它有效:)
【讨论】:
以上是关于PHP:从表单中获取电子邮件并在单击按钮时发送它的主要内容,如果未能解决你的问题,请参考以下文章