Page 1 of 1
php array from multiple values in input field
Posted: Wed Nov 23, 2011 1:32 am
by bizkit1
Hello. So there is a input field type text in my form where i have this values: dog,cat,horse.
Code: Select all
<input type="text" name="animals" id ="animals" size="40" />
When i submit the form i want php to make array for each of the words written in the input field. Can you please help me? Thanks
Re: php array from multiple values in input field
Posted: Wed Nov 23, 2011 1:38 am
by maxx99
http://php.net/manual/en/function.explode.php
Code: Select all
$str = 'one,two,three,four';
print_r(explode(',', $str));
Array
(
[0] => one
[1] => two
[2] => three
[3] => four
)
Is that what you need?
Re: php array from multiple values in input field
Posted: Wed Nov 23, 2011 1:53 am
by Gopesh
Hi,check this
Code: Select all
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
Animals:<input type="text" name="animals" id ="animals" size="40" />
<input type="submit" value="submit" name="submit" />
</form>
</body>
<?php
if(isset($_POST['submit']))
{
$animals=$_POST['animals'];
$arr=array();
$arr=explode(',',$animals);
echo $arr[0];
}
?>
</html>
Use explode() to split the string from the textbox.Hope it helps
Re: php array from multiple values in input field
Posted: Wed Nov 23, 2011 2:13 am
by bizkit1
thank you both.
@gopesh it only outputs the first word
Re: php array from multiple values in input field
Posted: Wed Nov 23, 2011 2:16 am
by maxx99
use
to see them all

or
Re: php array from multiple values in input field
Posted: Wed Nov 23, 2011 2:24 am
by bizkit1
it works..now here is another issue

So i have a form which i use to send emails. I have a field where i put the recipients email address, but i cand only send to one person. If i add multiple email addresses:
mail2@yahoo.com,
mail3@yahoo.com, the script says that the email address is wrong. I want it to loop through every email address added in the myemail field and if i add multiple addresses to be able to send to each one. here is my php script:
Code: Select all
<input type="text" name="myemail" id ="myemail" size="40" />
$to="$_POST[myemail]";
if(strlen($from)==0)
die("Please set the messages sender address in line ".$sender_line." of the script ".basename(__FILE__)."\n");
if(strlen($to)==0)
die("Please set the messages recipient address in line ".$recipient_line." of the script ".basename(__FILE__)."\n");
$smtp=new smtp_class;
$smtp->host_name="localhost"; /* Change this variable to the address of the SMTP server to relay, like "smtp.myisp.com" */
$smtp->host_port=25; /* Change this variable to the port of the SMTP server to use, like 465 */
$smtp->ssl=0; /* Change this variable if the SMTP server requires an secure connection using SSL */
$smtp->http_proxy_host_name=''; /* Change this variable if you need to connect to SMTP server via an HTTP proxy */
$smtp->http_proxy_host_port=3128; /* Change this variable if you need to connect to SMTP server via an HTTP proxy */
$smtp->socks_host_name = ''; /* Change this variable if you need to connect to SMTP server via an SOCKS server */
$smtp->socks_host_port = 1080; /* Change this variable if you need to connect to SMTP server via an SOCKS server */
$smtp->socks_version = '5'; /* Change this variable if you need to connect to SMTP server via an SOCKS server */
$smtp->start_tls=0; /* Change this variable if the SMTP server requires security by starting TLS during the connection */
$smtp->localhost="localhost"; /* Your computer address */
$smtp->direct_delivery=0; /* Set to 1 to deliver directly to the recepient SMTP server */
$smtp->timeout=10; /* Set to the number of seconds wait for a successful connection to the SMTP server */
$smtp->data_timeout=0; /* Set to the number seconds wait for sending or retrieving data from the SMTP server.
Set to 0 to use the same defined in the timeout variable */
$smtp->debug=0; /* Set to 1 to output the communication with the SMTP server */
$smtp->html_debug=0; /* Set to 1 to format the debug output as HTML */
$smtp->pop3_auth_host=""; /* Set to the POP3 authentication host if your SMTP server requires prior POP3 authentication */
$smtp->user=""; /* Set to the user name if the server requires authetication */
$smtp->realm=""; /* Set to the authetication realm, usually the authentication user e-mail domain */
$smtp->password=""; /* Set to the authetication password */
$smtp->workstation=""; /* Workstation name for NTLM authentication */
$smtp->authentication_mechanism=""; /* Specify a SASL authentication method like LOGIN, PLAIN, CRAM-MD5, NTLM, etc..
Leave it empty to make the class negotiate if necessary */
if($smtp->SendMessage(
$from,
array(
$to
),
array(
"From: $from",
"To: $to",
"Subject: Intact File Share",
"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
),
"Mail sent"))
else
echo "Error";
Re: php array from multiple values in input field
Posted: Wed Nov 23, 2011 2:37 am
by maxx99
General idea is the same
Code: Select all
...
$recipients = $explode(',',$to);
foreach($recipients as $recipient){
if($smtp->SendMessage(
$from,
array(
$to
),
array(
"From: $from",
"To: $recipient",//<----here
"Subject: Intact File Share",
"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
),
"Mail sent"))
else
echo "Error";
}
Try to avoid this kind of construct in if statement, hard to read.
Re: php array from multiple values in input field
Posted: Wed Nov 23, 2011 2:48 am
by bizkit1
it`s working, thank you all very very very much