如何将输入字段中的文本插入PHP数组

时间:2013-06-06 23:44:51

标签: php arrays post input sendgrid

我有一个带有输入字段的表单,当提交时,通过来自SendGrid的WebAPI发送电子邮件。没有写出整体来这里是重要的部分:

HTML:

<form action="sent.php" method="post">

<label>To:</label>

<input name="to" type="text" size="95" />

<label>Subject:</label>

<input name="subject" type="text" size="95" />

<label>Message:</label>

<textarea name="message" type="text" cols="71" rows="30"></textarea>

<input name="submit" type="submit" value="Send"/>

</form>

PHP:

<?php
require 'SendGrid_loader.php';

print "I'm setting up variables here\n";
$user = 'XXXX';
$password = 'XXXX';
$to_email = array('XXXX@yahoo.com','XXXX@gmail.com','XXXXhotmail.com');


print "I'm creating a new SendGrid account\n";
$sendgrid = new SendGrid($user,$password);


$mail = new SendGrid\Mail();
$mail->setTos($to_email)
    ->setFrom('jacob@jacob.com')
    ->setSubject($_POST['subject'])
    ->setText($_POST['message'])
    ->setFromName('Jacob');

print "about to send email\n";
$result=$sendgrid->web->send($mail);

print_r($result);

我要做的是允许某人在“收件人:”输入字段中输入多个电子邮件地址,用逗号分隔(即收件人:“john @ gmail.com,sue @ gmail.com,gill @ gmail。 com“)并将它们输入PHP文件中的数组。我试图这样做而不必创建多个To:input字段。

有什么建议吗?

谢谢

更新:

PHP explode()函数有效。这是我更新的工作代码:

HTML:

<form action="sent.php" method="post">

<label>To:</label>

<input name="to" type="text" size="95" />

<label>Subject:</label>

<input name="subject" type="text" size="95" />

<label>Message:</label>

<textarea name="message" type="text" cols="71" rows="30"></textarea>

<input name="submit" type="submit" value="Send"/>

</form>

PHP:

<?php
require 'SendGrid_loader.php';

print "I'm setting up variables here\n";
$user = 'XXXXXXX';
$password = 'XXXXXXX';
$str = $_POST['to'];
$to_email = explode(",", $str);


print "I'm creating a new SendGrid account\n";
$sendgrid = new SendGrid($user,$password);


$mail = new SendGrid\Mail();
$mail->setTos($to_email)
    ->setFrom('jacob@jacob.com')
    ->setSubject($_POST['subject'])
    ->setText($_POST['message'])
    ->setFromName('Jacob');

print "about to send email\n";
$result=$sendgrid->web->send($mail);

print_r($result);

1 个答案:

答案 0 :(得分:4)

http://php.net/explode 你有一个逗号分隔列表,你想变成一个数组?使用爆炸。