PDA

View Full Version : Need Help with PHP code for using an array checkbox on Form


bexterinni
09-07-2009, 07:40 PM
I know almost nothing about PHP. A friend helped me to customize an existing submit.php file for my website to use to submit a HTML form ("Contact Us") via email. The form submits fine and comes to me via email, and is formatted mostly fine except for the checkbox arrays. Instead of listing each of the items that the user checked, it simply lists "Array."

The HTML portion of the form containing the checkboxes looks like this:

<p>Which room(s) in your home is your project for? (Check all that apply.)</p>
<p>
<input name="room[]" type="checkbox" id="kitchen_floor" value="Kitchen Floor" />
Kitchen Floor&nbsp;&nbsp;

<input name="room[]" type="checkbox" id="kitchen_backsplash" value="Kitchen Backsplash" />
Kitchen Backsplash

<input name="room[]" type="checkbox" id="sunroom" value="Sunroom" />
Sunroom

<input name="room[]" type="checkbox" id="master_bath" value="Master Bath (wall or floor)" />
Master Bath (wall or floor)

<input name="room[]" type="checkbox" id="hall_bath" value="Hall Bath (wall or floor)" />
Hall Bath (wall or floor)&
<br/>
<input name="room[]" type="checkbox" id="powder_room" value="Powder Room" />
Powder Room

<input name="room[]" type="checkbox" id="laundry_room" value="Laundry Room" />
Laundry Room

<input name="room[]" type="checkbox" id="other" value="Other" />
Other
<input name="other_room" type="text" id="other_room" size="25" />
</p>

I know that my .php is missing the appropriate code for the array, but have no idea how to fix it. I *think* I need some kind of "foreach" code but don't know the proper way to write it, or where to put it.

Here's my .php code:

<?php
if (!isset($_POST) && isset($_POST)) {
$_POST = $_POST;
}

if (isset($_POST["submit"]) && ($_POST["submit"]=="Submit")
&& isset($_POST['subject']) && empty($_POST['subject'])) {

$sendto = "my.email@email.com";
$subject = "Inquiry from Website: Please Contact Me";
$successurl = "http://www.mywebsite.com/submit_success.html";
$SpamReplaceText = "*content removed*";
$SpamErrorMessage = "<p align=\"center\">It appears you entered an invalid value</p><p align=\"center\"><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";

$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$email = $_POST['email_address'];
$phone = $_POST['phone_number'];
$contact = $_POST['contact'];
$times = $_POST['times'];
$room = $_POST['room'];
$otherroom = $_POST['other_room'];
$project = $_POST['project'];
$squarefootage = $_POST['square_footage'];
$timing = $_POST['timing'];
$anythingelse = $_POST['anything_else'];

$headers = "From: $email\n";
$headers . "MIME-Version: 1.0\n"
. "Content-Transfer-Encoding: 7bit\n"
. "Content-type: text/html; charset = \"iso-8859-1\";\n\n";

// Build the email body text
$emailcontent = "
-----------------------------------------------------------------------------
$subject
-----------------------------------------------------------------------------

Customer's Name: $firstname $lastname

Customer's Email Address: $email

Customer's Phone Number: $phone

Customer prefers to be contacted via: $contact

Best time to call the Customer: $times

Room(s) that the project is for: $room

Room not in the list: $otherroom

Is this project new construction, a remodel, or a room update? $project

Approximate square footage of the project: $squarefootage

When the project will start: $timing

Is there anything else we should know about your project? $anythingelse
";
// Check that the email address entered matches the standard email address format
if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$", $email)) {
echo "<p>It appears you entered an invalid email address</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";
}

elseif (!trim($firstname)) {
echo "<p>Please go back and enter your First Name</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";
}

elseif (!trim($lastname)) {
echo "<p>Please go back and enter your Last Name</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";
}

elseif (!trim($email)) {
echo "<p>Please go back and enter your Email Address</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";
}
elseif (!trim($phone)) {
echo "<p>Please go back and enter your Phone Number</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";
}

// Sends out the email or will output the error message
elseif (mail($sendto, $subject, $emailcontent, $headers)) {
header( "Location: $successurl" );
exit ;
}
}
else {
header( "Location: $successurl" );
exit ;
}
?>

If anyone can give me very clear instruction how to fix my .php form, I'd greatly appreciate it.

~bexterinni

bee80
09-07-2009, 09:09 PM
Customer's Name: $firstname $lastname

Customer's Email Address: $email

Customer's Phone Number: $phone

Customer prefers to be contacted via: $contact

Best time to call the Customer: $times

Room(s) that the project is for: foreach($room as $listrooms) {

$listrooms\n }
Room not in the list: $otherroom

Is this project new construction, a remodel, or a room update? $project

Approximate square footage of the project: $squarefootage



something like this? let me know if it works

bexterinni
09-07-2009, 09:39 PM
Thanks bee80, but that didn't work. Here's what I got back for the two checkbox questions:



Room(s) that the project is for: foreach(Other as ) {

}

Room not in the list: porch

Is this project new construction, a remodel, or a room update? foreach(Updating a room as ) {

}

bee80
09-13-2009, 10:38 AM
ok take the foreach loop out the $emailcontent variable first then and make it build a new variable called $totalroom :



$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$email = $_POST['email_address'];
$phone = $_POST['phone_number'];
$contact = $_POST['contact'];
$times = $_POST['times'];
$room = $_POST['room'];
foreach($room as $listroom) {
@$totalroom .= "$listroom\n";
}
$otherroom = $_POST['other_room'];
$project = $_POST['project'];
$squarefootage = $_POST['square_footage'];
$timing = $_POST['timing'];
$anythingelse = $_POST['anything_else'];


then post $totalroom in $emailcontent variable :


// Build the email body text
$emailcontent = "
-----------------------------------------------------------------------------
$subject
-----------------------------------------------------------------------------

Customer's Name: $firstname $lastname

Customer's Email Address: $email

Customer's Phone Number: $phone

Customer prefers to be contacted via: $contact

Best time to call the Customer: $times

Room(s) that the project is for: $totalroom

Room not in the list: $otherroom

Is this project new construction, a remodel, or a room update? $project

Approximate square footage of the project: $squarefootage "

bexterinni
09-15-2009, 02:12 PM
Thank you again bee80. The friend who was helping me with the original file came up with a workable solution for my website. Here is the corrected code that he wrote for my site:


<?php

if (!isset($HTTP_POST_VARS) && isset($_POST)) {
$HTTP_POST_VARS = $_POST;
}

if (isset($HTTP_POST_VARS["submit"]) && ($HTTP_POST_VARS["submit"]=="Submit")
&& isset($HTTP_POST_VARS['subject']) && empty($HTTP_POST_VARS['subject'])) {

$sendto = "my.email@email.com";
$subject = "Inquiry from Website: Please Contact Me";
$successurl = "http://www.mywebsite.com/submit_success.html";
$SpamReplaceText = "*content removed*";
$SpamErrorMessage = "<p align=\"center\">It appears you entered an invalid value</p><p align=\"center\"><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";

$firstname = $HTTP_POST_VARS['first_name'];
$lastname = $HTTP_POST_VARS['last_name'];
$email = $HTTP_POST_VARS['email_address'];
$phone = $HTTP_POST_VARS['phone_number'];
$contact = $HTTP_POST_VARS['contact'];
$times = $HTTP_POST_VARS['times'];
$room = $HTTP_POST_VARS['room'];
$otherroom = $HTTP_POST_VARS['other_room'];
$project = $HTTP_POST_VARS['project'];
$squarefootage = $HTTP_POST_VARS['square_footage'];
$timing = $HTTP_POST_VARS['timing'];
$anythingelse = $_POST['anything_else'];

$headers = "From: $email\n";
$headers . "MIME-Version: 1.0\n"
. "Content-Transfer-Encoding: 7bit\n"
. "Content-type: text/html; charset = \"iso-8859-1\";\n\n";

// Build the email body text
$emailcontent = "
-----------------------------------------------------------------------------
$subject
-----------------------------------------------------------------------------

Customer's Name: $firstname $lastname

Customer's Email Address: $email

Customer's Phone Number: $phone

Customer prefers to be contacted via: $contact

Best time to call the Customer: $times

Room(s) that the project is for: ";

foreach($room as $value) { $emailcontent .= "$value, "; }

$emailcontent .= "

Room not in the list: $otherroom

Is this project new construction, a remodel, or a room update? ";

foreach($project as $value) { $emailcontent .= "$value, "; }

$emailcontent .= "

Approximate square footage of the project: $squarefootage

When the project will start: $timing

Is there anything else we should know about your project? $anythingelse
";

// Check that the email address entered matches the standard email address format
if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$", $email)) {
echo "<p>It appears you entered an invalid email address</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";
}

elseif (!trim($firstname)) {
echo "<p>Please go back and enter your First Name</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";
}

elseif (!trim($lastname)) {
echo "<p>Please go back and enter your Last Name</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";
}

elseif (!trim($email)) {
echo "<p>Please go back and enter your Email Address</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";
}
elseif (!trim($phone)) {
echo "<p>Please go back and enter your Phone Number</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";
}

// Sends out the email or will output the error message
elseif (mail($sendto, $subject, $emailcontent, $headers)) {
header( "Location: $successurl" );
exit ;
}
}
else {
header( "Location: $successurl" );
exit ;
}
?>