Page 1 of 1

Getting input and putting it into an array

Posted: Sun Dec 07, 2014 7:54 pm
by RichGags
Hi. I subscribe to a text messaging service. I am writing a php script to accept input from a field for a phone number. I would like to pass that to the next page and have it go into an array. So if I input the phone numbers separated by commas, it would send my messages one at a time.

Currently my input page looks like this...

Code: Select all

<form action="sendit.php" method="post" name="form1">
<input type="text" name="phone" value="" size="160">
<input type="submit" value="Submit" name="submit">
Then in sendit.php I have...

Code: Select all


  $people = array(
        "+19145551111",
    );
What would I need to do to grab the input which could look like...

Code: Select all

+19145551111,+19145551212,+19145551313,+19145551414
and make the $people array look like this...

Code: Select all

 $people = array(
        "+19145551111",
        "+19145551212",
        "+19145551313",
        "+19145551414",
    );

Right now I am hard-coding the numbers when I need to send out bulk messages but I would rather do it via a input form page. Thanks!

Re: Getting input and putting it into an array

Posted: Sun Dec 07, 2014 9:46 pm
by requinix
explode

Remember to validate the input.

Re: Getting input and putting it into an array

Posted: Fri Jan 30, 2015 8:10 am
by RichGags
Perfect! Thanks so much.

Re: Getting input and putting it into an array

Posted: Fri Jan 30, 2015 9:06 am
by liljester
jeffreyappel's solution seems a little excessive.

you could just stop at:

Code: Select all

$people = explode(',', $_POST['phone']);
and as was suggested, you should loop through the array and do any validation required on its values.

Re: Getting input and putting it into an array

Posted: Fri Jan 30, 2015 2:46 pm
by requinix
liljester wrote:jeffreyappel's solution seems a little excessive.
You're too nice. I would have said "wasteful" or "pointless".