Tuesday, March 30, 2010

PHP scripting for form with multiple...

I am new to Dreamweaver and PHP scripting. I am having difficulty scripting a form in PHP that allows selection of multiple checkboxes. The only one that comes through is the last checkbox selected in the list in the cruise and land section. No selections show in destination, activities or referred field, although they are scripted the same as far as I can tell. However if I put a name in the friend textbox that does show.

I have searched the internet, but nothing seems to work. I have tried checkbox array in form handling. But it still doesn't work. Anyone able to assist me? I appreciate any help.

You can see the form at www.iconfortravel.com/contact.html.

Below is the PHP scripting I am using for the form.



%26lt;?php

/* Subject and Email Variables */

$emailSubject = 'Icon For Travel Information Request Form!';
$webMaster = 'judy@iconfortravel.com';

/* Gathering Data Variables */

$emailField = $_POST['email'];
$nameField = $_POST['name'];
$phoneField = $_POST['phone'];
$commentsField = $_POST['comments'];
$newletterField = $_POST['newsletter'];
$favoriteField = $_POST['favorite'];
$cruisesField = $_POST['cruises'];
$preferredField = $_POST['preferred'];
$landField = $_POST['land'];
$destinationsField = $_POST['destinations'];
$activitiesField = $_POST['activities'];
$referredField = $_POST['referred'];

$body = %26lt;%26lt;%26lt;EOD
%26lt;br%26gt;%26lt;hr%26gt;%26lt;br%26gt;
Email: $email %26lt;br%26gt;
Name: $name %26lt;br%26gt;
Phone Number: $phone %26lt;br%26gt;
Comments: $comments %26lt;br%26gt;
Newletter: $newsletter %26lt;br%26gt;
Favorite Vacation: $favorite %26lt;br%26gt;
Type of Cruise Vacation: $cruises %26lt;br%26gt;
Preferred Cruise Lines: $preferred %26lt;br%26gt;
Type of Land Vacation: $land %26lt;br%26gt;
Destinations of Interest: $destinations %26lt;br%26gt;
Activities of Interest: $activities %26lt;br%26gt;
Referred by: $referred %26lt;br%26gt;
Referred friend: $referredfriend %26lt;br%26gt;
EOD;

$headers = ''From: $email\r\n'';
$headers .= ''Content-type: text/html\r\n'';
$success = mail($webMaster, $emailSubject, $body, $headers);

/* Results rendered as HTML */

$theResults = %26lt;%26lt;%26lt;EOD
%26lt;html%26gt;
%26lt;head%26gt;
%26lt;title%26gt;Icon For Travel -Homepage%26lt;/title%26gt;
%26lt;meta http-equiv=''Content-Type'' content=''text/html; charset=utf-8''%26gt;
%26lt;style type=''text/css''%26gt;
%26lt;!--
body {
background-color: #31887D;
font-family: Palatino Linotype, Book Antiqua, Palatino, serif
font-size: 18px;
font-style: normal;
line-height: normal;
font-weight: bold;
color: #FFF;
text-decoration: none;
}
body,td,th {
font-size: 18px;
}
--%26gt;
%26lt;/style%26gt;
%26lt;/head%26gt;%26lt;body text=''#FFF''%26gt;

%26lt;div%26gt;
?%26lt;div align=''left''%26gt;Thank you for your interest! Your email will be answered very soon!%26lt;/div%26gt;
%26lt;/div%26gt;
%26lt;/body%26gt;
%26lt;/html%26gt;
EOD;
echo ''$theResults'';

?%26gt;

PHP scripting for form with multiple...

Each checkbox should have a different name and you should have a php variable set for each POST value for each checkbox name. Also correct your email script to avoid email header injection.

PHP scripting for form with multiple...

Thank you so much for your quick reply. I have been working on trying to fix this for 2 days!

Each checkbox does have its own name i.e., cruises_0, cruises_1, cruises_2, etc. Is that okay or do I need to rename to something else?

I am not sure what you mean by ''have a php variable set for each POST value for each checkbox name. I think I might have done this in one of the other attempts. Is this what you mean?

%26lt;?php

/* Subject and Email Variables */

$emailSubject = 'Icon For Travel Information Request Form!';
$webMaster = 'judy@iconfortravel.com';

/* Gathering Data Variables */

$emailField = $_POST['email'];
$nameField = $_POST['name'];
$phoneField = $_POST['phone'];
$commentsField = $_POST['comments'];
$newletterField = $_POST['newsletter'];
$favoriteField = $_POST['favorite'];
$cruisesField = $_POST['cruises'];
$preferredField = $_POST['preferred'];
$landField = $_POST['land'];
$destinationsField = $_POST['destinations'];
$activitiesField = $_POST['activities'];
$referredField = $_POST['referred'];

$body = %26lt;%26lt;%26lt;EOD
%26lt;br%26gt;%26lt;hr%26gt;%26lt;br%26gt;
Email: $email %26lt;br%26gt;
Name: $name %26lt;br%26gt;
Phone Number: $phone %26lt;br%26gt;
Comments: $comments %26lt;br%26gt;
Newletter: $newsletter %26lt;br%26gt;
Favorite Vacation: $favorite %26lt;br%26gt;
Type of Cruise Vacation: $cruises %26lt;br%26gt;
%26lt;form action=''checkbox.php'' method=''post''%26gt;

%26lt;input type=''checkbox'' name=''cruises[]'' value=''0'' /%26gt;All cruise offers %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''cruises[]'' value=''1'' /%26gt;River cruises %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''cruises[]'' value=''2'' /%26gt;Large ships %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''cruises[]'' value=''3'' /%26gt;Small ships %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''cruises[]'' value=''4'' /%26gt;Quick getaways/short notice %26lt;br%26gt;
Preferred Cruise Lines: $preferred %26lt;br%26gt;
Type of Land Vacation: $land %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''land[]'' value=''0'' /%26gt;All land offers %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''land[]'' value=''1'' /%26gt;All inclusives %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''land[]'' value=''2'' /%26gt;Escorted %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''land[]'' value=''3'' /%26gt;Custom %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''land[]'' value=''4'' /%26gt;Quick getaways/short notice %26lt;br%26gt;
Destinations of Interest: $destinations %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''0'' /%26gt;Africa %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''1'' /%26gt;Alaska %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''2'' /%26gt;Antartica %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''3'' /%26gt;Asia/Far East %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''4'' /%26gt;Australia/New Zealand %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''5'' /%26gt;Bahamas %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''6'' /%26gt;Baltic %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''7'' /%26gt;Bermuda %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''8'' /%26gt;Black Sea %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''9'' /%26gt;Canada %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''10'' /%26gt;Caribbean %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''11'' /%26gt;Cental America %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''12'' /%26gt;Eastern Europe %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''13'' /%26gt;Florida %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''14'' /%26gt;Hawaii %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''15'' /%26gt;India %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''16'' /%26gt;Las Vegas %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''17'' /%26gt;Mediterranean %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''18'' /%26gt;Mexico %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''19'' /%26gt;Middle East %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''20'' /%26gt;New England %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''21'' /%26gt;Panama %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''22'' /%26gt;South America %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''23'' /%26gt;South Pacific %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''24'' /%26gt;Tahiti %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''25'' /%26gt;USA %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''26'' /%26gt;Western Europe %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''destinations[]'' value=''27'' /%26gt;Other %26lt;br%26gt;%26lt;br /%26gt;
Other destinations: $otherdestinations %26lt;br%26gt;
Activities of Interest: $activities %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''0'' /%26gt;Adventure %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''1'' /%26gt;Art %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''2'' /%26gt;Beach %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''3'' /%26gt;Bicycling %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''4'' /%26gt;Couples only %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''5'' /%26gt;Cultural travel %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''6'' /%26gt;Disney %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''7'' /%26gt;Family %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''8'' /%26gt;Fishing %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''9'' /%26gt;Fitness %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''10'' /%26gt;Food %26amp; Wine %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''11'' /%26gt;Golf %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''12'' /%26gt;Hiking %26amp; Walking %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''13'' /%26gt;History %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''14'' /%26gt;Honeymoon/romance %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''15'' /%26gt;Horseback riding %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''16'' /%26gt;Music %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''17'' /%26gt;RV %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''18'' /%26gt;Safari %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''19'' /%26gt;Sailing %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''20'' /%26gt;Scuba/snorkeling %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''21'' /%26gt;Shopping %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''22'' /%26gt;Snow ski %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''23'' /%26gt;Spa %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''24'' /%26gt;Surfing %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''25'' /%26gt;Tennis %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''26'' /%26gt;Theater %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''27'' /%26gt;Villa/private homes %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''28'' /%26gt;Water sports %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''29'' /%26gt;Whale watching %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''activities[]'' value=''30'' /%26gt;Other %26lt;br%26gt;
Other activities: $otheractivities %26lt;br%26gt;
Referred by: $referred %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''referred[]'' value=''0'' /%26gt;Advertisement %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''referred[]'' value=''1'' /%26gt;Email %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''referred[]'' value=''2'' /%26gt;Repeat customer %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''referred[]'' value=''3'' /%26gt;Search Engine result %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''referred[]'' value=''4'' /%26gt;Yellow page search %26lt;br%26gt;
%26lt;input type=''checkbox'' name=''referred[]'' value=''5'' /%26gt;Friend %26lt;br%26gt;
Referred friend: $referredfriend %26lt;br%26gt;
EOD;

/* and in your checkbox.php you do this: */

if(isset($_POST['Submit']))
{
for ($i=0; $i%26lt;count($_POST['checkbox']);$i++) {
echo ''%26lt;br /%26gt;value $i = ''.$_POST['checkbox'][$i];
}
}

$headers = ''From: $email\r\n'';
$headers .= ''Content-type: text/html\r\n'';
$success = mail($webMaster, $emailSubject, $body, $headers);

/* Results rendered as HTML */

$theResults = %26lt;%26lt;%26lt;EOD
%26lt;html%26gt;
%26lt;head%26gt;
%26lt;title%26gt;Icon For Travel -Homepage%26lt;/title%26gt;
%26lt;meta http-equiv=''Content-Type'' content=''text/html; charset=utf-8''%26gt;
%26lt;style type=''text/css''%26gt;
%26lt;!--
body {
background-color: #31887D;
font-family: Palatino Linotype, Book Antiqua, Palatino, serif
font-size: 18px;
font-style: normal;
line-height: normal;
font-weight: bold;
color: #FFF;
text-decoration: none;
}
body,td,th {
font-size: 18px;
}
--%26gt;
%26lt;/style%26gt;
%26lt;/head%26gt;%26lt;body text=''#FFF''%26gt;

%26lt;div%26gt;
?%26lt;div align=''left''%26gt;Thank you for your interest! Your email will be answered very soon!%26lt;/div%26gt;
%26lt;/div%26gt;
%26lt;/body%26gt;
%26lt;/html%26gt;
EOD;
echo ''$theResults'';


?%26gt;

If so, in this case, all the boxes showed on my response email, but there still were no check marks in the boxes. Only the last item selected showed after the semicolon for the Type of Cruise (or land, destination, referred) field.

Where do I position the following in my script to avoid email header injection? After my email address? Do I need any semicolon or other coding (separate line)?

if ( ereg( ''[\r\n]'', $name ) || ereg( ''[\r\n]'', $email ) ) {

[... direct user to an error page and quit ...]

}

Here's a little snippet of what I'm talking about where the check box names are all the same. Different ID's, same name


?%26lt;input type=''checkbox'' name=''cruises'' value=''all cruise offers'' id=''cruises_0'' /%26gt;
?All cruise offers%26lt;/label%26gt;%26lt;/td%26gt;
?%26lt;/tr%26gt;
?%26lt;tr%26gt;
?%26lt;td%26gt;%26lt;label%26gt;
?%26lt;input type=''checkbox'' name=''cruises'' value=''river cruises'' id=''cruises_1'' /%26gt;
?River cruises%26lt;/label%26gt;%26lt;/td%26gt;
?%26lt;/tr%26gt;
?%26lt;tr%26gt;
?%26lt;td%26gt;%26lt;label%26gt;
?%26lt;input type=''checkbox'' name=''cruises'' value=''large ships'' id=''cruises_2'' /%26gt;
?Large ships%26lt;/label%26gt;%26lt;/td%26gt;
?%26lt;/tr%26gt;
?%26lt;tr%26gt;
?%26lt;td%26gt;%26lt;label%26gt;
?%26lt;input type=''checkbox'' name=''cruises'' value=''small ships'' id=''cruises_3'' /%26gt;
?Small ships%26lt;/label%26gt;

And for setting variables for each POST value you'd add them here where cruises_2, cruises_3, cruises_4, etc. = the NAME of the form input:

/* Gathering Data Variables */

$emailField = $_POST['email'];
$nameField = $_POST['name'];
$phoneField = $_POST['phone'];
$commentsField = $_POST['comments'];
$newletterField = $_POST['newsletter'];
$favoriteField = $_POST['favorite'];


$cruisesField = $_POST['cruises'];



$cruises2Field = $_POST['cruises_2'];

$cruises3Field = $_POST['cruises_3'];

$cruises4Field = $_POST['cruises_4'];


$preferredField = $_POST['preferred'];
$landField = $_POST['land'];
$destinationsField = $_POST['destinations'];
$activitiesField = $_POST['activities'];
$referredField = $_POST['referred'];

Also correct your email script to avoid email header injection.

No comments:

Post a Comment