如何解决从文本区域获取多个电子邮件地址
我有一个问题:我需要创建一个带有文本区域的POST方法表单,用户可以在其中复制粘贴多个电子邮件地址,并且我的表单应该能够分别处理每个电子邮件地址。 而且我不知道如何告诉PHP与textarea的POST值分开识别每个电子邮件地址,并将它们插入到我的数据库表中。例如
所以我的代码目前看起来像这样:
<?PHP
if($_POST) {
if(!empty($_POST['emails'])) {
$emails = explode(" ",$_POST['Emails']);
foreach($emails as $email) {
/* Do something with each adresses ( like inserting them into a
table in my database for example) */
}
}
}
?>
<html>
<form class="mx-auto" method="POST" id="invitations">
<div class="form-group w-50 mx-auto text-center">
<label for="emails">Insert the email adresses</label>
<textarea name="emails" id="emails"></textarea>
<button id="send-data" class="btn btn-primary mx-auto my-5 text-center">Send the invitations</button>
</div>
</form>
</html>
解决方法
<?php
if(!empty($_POST['Emails'])) {
$emails = explode(" ",$_POST['Emails']);
foreach($emails as $email) {
if(filter_var($email,FILTER_VALIDATE_EMAIL)) {
// Do something
} else {
echo "This email adress is not valid => $email !";
return;
/* here if something else than a space or a valid email adress is inserted
the foreach will be exited and a error message specifying where
something was wrong is displayed :) */
}
}
} else {
$feedback = "Please insert one or multiple email adress(es)";
}
if(isset($feedback) and !empty($feedback)) {
echo $feedback;
}
?>
<html>
<form class="mx-auto" method="POST" id="invitations">
<div class="form-group w-50 mx-auto text-center">
<label for="emails">Insert the email adresses</label>
<small>Separate them with a space</small>
<textarea name="emails" id="emails"></textarea>
<button id="send-data" class="btn btn-primary mx-auto my-5 text-center">Send the invitations</button>
</div>
</form>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。