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

时间:2022-08-19 09:52:08

I have a form with input fields that when submited sends an email out through a WebAPI from SendGrid. Without writing out the whole come here are the important parts:

HTML:

我有一个带有输入字段的表单,当提交时,通过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);

What I am trying to do is allow someone to enter multiple email addresses in the "To:" input field seperate by a comma (i.e. To: "john@gmail.com, sue@gmail.com, gill@gmail.com") and have them entered into the array in the PHP file. I am trying to do this without having to create more than one To: input field.

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

Any suggestions?

Thank You

!UPDATE:

The PHP explode() Function worked. Here is my updated working code:

HTML:

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 个解决方案

#1


4  

http://php.net/explode You have a comma separated list you want to turn into an array? Use explode.

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

#1


4  

http://php.net/explode You have a comma separated list you want to turn into an array? Use explode.

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