I cut my teeth on DHTML ("Dynamic" HTML using javascript or Microsoft's jscript) when I started coding in 1995 when everything happened on the client browser. With the blazing speed of 56K modems you couldn't really do too much server-side without causing your users endless frustration while waiting for a page refresh with new data. It has been interesting to see the movement from the client to the server with ASP and then .NET (and PHP, of course) as connectivity improved. Today we've come full circle as javascript is once again the language of choice clent-side on mobile platforms like ipads and mobile phones. 3G is much faster than 56K modems but we still want to limit unnecessary calls to the server. I now live in jQuery and backbone.js libraries and develop sites and apps using HTML5 responsive design with media queries where the size of the screen determines layout so that one site will render across almost all platforms (test my new, unfinished, portfolio page by dragging the corner of the browser to ever smaller widths to see the effect).
I do somewhat less PHP programming than most developers but occasionally I run into a puzzler (at least to me) as happened this week. A political client wanted a simple PHP form to post to a database and send two emails. That was fairly straightforward. But one of the form fields allowed multiple selections.
I dove into my favorite jQuery library and tossed in some live validation and tested the post grabbing the multiselect field named xschool:
<?php $xschool = $_POST['xschool']; ?>
But when I went to "echo" the variables I received the text string "array". Ooops.
Allright, all I had to do is this, right?
if ($xschool) {
foreach ($xschool as $d){echo $d;}
}
And, sure enough, when I tested the form and did a simple "print" or "echo" using the code I indeed all of the schools selected by the user in the original form were displayed.
So, I grabbed the variable "d" and stuck it into the email and database code where it immediately failed and returned the text string "array" again.
$ToEmail = "".$_POST["xemail"]."";
$EmailSubject = 'Constituent Survey Responses';
$mailheader = "From: survey@client.com\r\n";
$mailheader .= "BCC: client@client.com\r\n";
$mailheader .= "Reply-To: client@client.com\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = '<html><body>';
$MESSAGE_BODY .= '<p>Thank you for completing the survey.<br />Here are your responses:</p>';
$MESSAGE_BODY .= "<p>e-mail: ".$xemail."<br>";
$MESSAGE_BODY .= "Zipcode: ".$xzipcode."<br>";
$MESSAGE_BODY .= "Schools Selected: ".$d."<br>";
$MESSAGE_BODY .= "Comment: ".nl2br($xcomments)."</p>";
$MESSAGE_BODY2 .= "</body></html>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
Well, after fiddling around for 20 minutes I was stumped. How does one grab and array and use it as a variable? It seemed simple enough. Then I remembered "explode and implode". First I needed to grab the posted data from the multiselect field as an array:
if ($xschool)
{
foreach ($xschool as $schools)
{
$d[] = $schools ;
}
}
Then I needed to "implode" the array:
$MESSAGE_BODY .= "Schools selected: ".implode(', ',$d)."<br>";
In PHP the implode() function returns a string from the elements of an array using the syntax implode(separator,array). Although the implode() function accept its parameters in either order, for consistency with explode(), you should use the documented order of arguments. The separator parameter of implode() is optional. However, it is recommended to always use two parameters for backwards compatibility. Example:
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
The output of the code above will be: Hello World! Beautiful Day!
Here is a working example of the form for you to test: