NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page php form process multiple checkboxes

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Reply
 
LinkBack Thread Tools
Old 08-22-2010, 02:18 PM THREAD STARTER               #1 (permalink)
Senior Member
 
NameCharger's Avatar
Join Date: Mar 2007
Posts: 1,708
NameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud of
 


Child Abuse Animal Rescue Autism Child Abuse Protect Our Planet Autism Protect Our Planet

Help! php form process multiple checkboxes


Hello,

Trying to process a form with 2 sets of checkboxes and sending results in email but getting stumped.
????: NamePros.com http://www.namepros.com/programming/673448-php-form-process-multiple-checkboxes.html

In the processing page, I'm scrubbing data with function check_input then inserting it into the email body.

As is, the script is returning bad arguments and cannot modify header info. The culprits:

$flooringType = implode(', ', $_POST['flooringType']);
$roomsFlooring = implode(', ', $_POST['$roomsFlooring']);

If I use only 1 implode statement, the script works and properly parses data, but with both implode statements in there it errors out.

process.php:
Code:
<?php
/* Scrub form input using check_input function */
$firstName = check_input($_POST['firstName']);
$lastName = check_input($_POST['lastName']);
$address  = check_input($_POST['address']);
$city = check_input($_POST['city']);
$zip = check_input($_POST['zip']);
$crossStreets = check_input($_POST['crossStreets']);
$phone = check_input($_POST['phone']);
$areaCode = check_input($_POST['areaCode']);
$phone1 = check_input($_POST['phone1']);
$phone2 = check_input($_POST['phone2']);
$cell = check_input($_POST['cell']);
$email = check_input($_POST['email']);
$otherDetails = check_input($_POST['otherDetails']);
$apptDate = check_input($_POST['apptDate']);
$apptTime = check_input($_POST['apptTime']);
$comments = check_input($_POST['comments']);

/* Handle checkbox arrays */
$flooringType = implode(', ', $_POST['flooringType']);
$roomsFlooring = implode(', ', $_POST['$roomsFlooring']); 

/* Send the message using mail() function */
mail("...edited on purpose to save forum space" );

/* Redirect visitor to the thank you page */
header('Location: thankyou.php');
exit();

/* Functions to scrub data and error message for required form fields */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>

<script language="javascript" type="text/javascript">
alert('Please fill in the following information:\n\n<?php echo $myError; ?>');
onclick = history.back(); 
</script>
<noscript>Please fill in the following information:
<?php echo $myError; ?></noscript>

<?php
exit();
}
?>
Your input is greatly appreciated!
Last edited by NameCharger; 08-22-2010 at 04:57 PM.
NameCharger is offline   Reply With Quote
Old 08-22-2010, 02:54 PM   #2 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
$roomsFlooring = $_POST['$roomsFlooring'] = implode(', ', $_POST['$roomsFlooring']);


See the problem? =)
Eric is offline   Reply With Quote
Old 08-22-2010, 05:00 PM THREAD STARTER               #3 (permalink)
Senior Member
 
NameCharger's Avatar
Join Date: Mar 2007
Posts: 1,708
NameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud of
 


Child Abuse Animal Rescue Autism Child Abuse Protect Our Planet Autism Protect Our Planet
Originally Posted by Eric View Post
$roomsFlooring = $_POST['$roomsFlooring'] = implode(', ', $_POST['$roomsFlooring']);
????: NamePros.com http://www.namepros.com/showthread.php?t=673448


See the problem? =)
Thanks for the reply. I took out what you suggested and it's still bugging out. I cleaned up the code in my OP to reflect must current version.
NameCharger is offline   Reply With Quote
Old 08-22-2010, 05:46 PM   #4 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Untested, and off the top of my head, but maybe something like this:

PHP Code:
<?php

/* Function to scrub data */
function scrub_input($value)
{
    if (
is_array($value))
    {
        foreach (
$value AS $key => $val)
        {
            if (
is_string($val))
            {
                
$value["$key"] = trim(stripslashes(htmlspecialchars($val)));
            }
            else if (
is_array($val))
            {
????: NamePros.com http://www.namepros.com/showthread.php?t=673448
                
$value["$key"] = scrub_input($value["$key"]);
            }
        }
        return 
$value;
    }
    return 
trim(stripslashes(htmlspecialchars($value)));
}

/* Scrub form input using check_input function */
$_POST scrub_input($_POST);

/* required fields */
$errors = array();

$required = array(
    
'firstName',
????: NamePros.com http://www.namepros.com/showthread.php?t=673448
    
'lastName',
    
'address',
    
'city',
    
'zip',
    
'crossStreets',
    
'phone',
    
'areaCode',
    
'phone1',
    
'phone2',
    
'cell',
    
'email',
    
'otherDetails',
    
'apptDate',
    
'apptTime',
    
'comments'
);

foreach (
$_POST AS $key => $val)
{
    if (isset(
$required[$key]) AND strlen($val) == 0)
    {
        
$errors[] = "$key is required";
    }
    $
$key = (is_array($val) ? implode(', '$val) : $val);
}

if (
count($errors) > 0)
{
    echo 
"One or more errors occurred:<br />\n<ul>\n";

    foreach (
$errors AS $error)
    {
        echo 
"<li>$error</li>\n";
    }

    echo 
"</ul>\n";
    exit;
}
else
{
    
/* Send the message using mail() function */
    
if (@mail($to$subject$message$headers))
    {
        
header('Location: thankyou.php');
        exit;
    }
    else
    {
        echo 
'Sorry, mail could not be sent.';
    }
}

?>
Eric is offline   Reply With Quote
Old 08-22-2010, 05:53 PM   #5 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Originally Posted by Eric View Post
$value["$key"]

????: NamePros.com http://www.namepros.com/showthread.php?t=673448
bit of topic but why are you using the double quotes when the only thing you are entering is the variable?
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline   Reply With Quote
Old 08-22-2010, 06:35 PM THREAD STARTER               #6 (permalink)
Senior Member
 
NameCharger's Avatar
Join Date: Mar 2007
Posts: 1,708
NameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud of
 


Child Abuse Animal Rescue Autism Child Abuse Protect Our Planet Autism Protect Our Planet
Thanks for the info.

Here's what I have now:
PHP Code:
/* mail info */
$to "--edited--";
$subject "test";
$message "--edited-- ";
$headers 'From: --edited--' "\r\n" .
    
'Reply-To: --edited--' "\r\n" .
    
'X-Mailer: PHP/' phpversion();
    
/* Function to scrub data */
function scrub_input($value)
{
    if (
is_array($value))
    {
        foreach (
$value AS $key => $val)
        {
            if (
is_string($val))
            {
                
$value[$key'] = trim(stripslashes(htmlspecialchars($val)));
            }
            else if (is_array($val))
            {
                $value['
$key'] = scrub_input($value['$key']);
            }
        }
        return $value;
    }
    return trim(stripslashes(htmlspecialchars($value)));
????: NamePros.com http://www.namepros.com/showthread.php?t=673448
}

/* Scrub form input using check_input function */
$_POST = scrub_input($_POST);

/* required fields */
$errors = array();

$required = array(
    '
firstName',
    '
lastName',
    '
address',
    '
city',
    '
zip',
    '
crossStreets',
    '
phone',
    '
areaCode',
    '
phone1',
    '
phone2',
    '
cell',
    '
email',
    '
otherDetails',
    '
apptDate',
    '
apptTime',
    '
comments'
);

foreach ($_POST AS $key => $val)
{
    if (isset($required[$key]) AND strlen($val) == 0)
    {
        $errors[] = "$key is required";
    }
    $$key = (is_array($val) ? implode('
', $val) : $val);
}

if (count($errors) > 0)
{
    echo "One or more errors occurred:<br />\n<ul>\n";

    foreach ($errors AS $error)
    {
        echo "<li>$error</li>\n";
    }

    echo "</ul>\n";
    exit;
}
else
{
    /* Send the message using mail() function */
    if (@mail($to, $subject, $message, $headers))
????: NamePros.com http://www.namepros.com/showthread.php?t=673448
    {
        header('
Locationthankyou.php');
        exit;
    }
    else
    {
        echo '
Sorrymail could not be sent.';
    }
}

?> 
Just tested this and all the fields are successfully being sent except the 2 checkbox fields are showing "Array" in the email and I can send a blank form through (errors aren't triggering).

Any suggestions?

Thanks in advance.
NameCharger is offline   Reply With Quote
Old 08-22-2010, 11:32 PM   #7 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Originally Posted by NameCharger View Post
Thanks for the info.

Here's what I have now:
PHP Code:
/* mail info */
$to "--edited--";
$subject "test";
$message "--edited-- ";
$headers 'From: --edited--' "\r\n" .
    
'Reply-To: --edited--' "\r\n" .
    
'X-Mailer: PHP/' phpversion();
    
/* Function to scrub data */
function scrub_input($value)
{
    if (
is_array($value))
    {
        foreach (
$value AS $key => $val)
        {
            if (
is_string($val))
            {
                
$value[$key'] = trim(stripslashes(htmlspecialchars($val)));
            }
            else if (is_array($val))
????: NamePros.com http://www.namepros.com/showthread.php?t=673448
            {
                $value['
$key'] = scrub_input($value['$key']);
            }
        }
        return $value;
    }
    return trim(stripslashes(htmlspecialchars($value)));
}

/* Scrub form input using check_input function */
$_POST = scrub_input($_POST);

/* required fields */
$errors = array();

$required = array(
    '
firstName',
    '
lastName',
    '
address',
    '
city',
    '
zip',
    '
crossStreets',
    '
phone',
    '
areaCode',
    '
phone1',
    '
phone2',
    '
cell',
    '
email',
    '
otherDetails',
    '
apptDate',
    '
apptTime',
    '
comments'
);

foreach ($_POST AS $key => $val)
{
    if (isset($required[$key]) AND strlen($val) == 0)
    {
        $errors[] = "$key is required";
    }
    $$key = (is_array($val) ? implode('
', $val) : $val);
}

if (count($errors) > 0)
{
    echo "One or more errors occurred:<br />\n<ul>\n";

    foreach ($errors AS $error)
    {
????: NamePros.com http://www.namepros.com/showthread.php?t=673448
        echo "<li>$error</li>\n";
    }

    echo "</ul>\n";
    exit;
}
else
{
    /* Send the message using mail() function */
    if (@mail($to, $subject, $message, $headers))
    {
        header('
Locationthankyou.php');
        exit;
    }
    else
    {
        echo '
Sorrymail could not be sent.';
    }
}

?> 
Just tested this and all the fields are successfully being sent except the 2 checkbox fields are showing "Array" in the email and I can send a blank form through (errors aren't triggering).

Any suggestions?

Thanks in advance.
This should actually be giving out parse errors. replace $value['$key'] with $value[$key] and also replace $value[$key'] with $value[$key]
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline   Reply With Quote
Old 08-23-2010, 09:59 AM THREAD STARTER               #8 (permalink)
Senior Member
 
NameCharger's Avatar
Join Date: Mar 2007
Posts: 1,708
NameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud ofNameCharger has much to be proud of
 


Child Abuse Animal Rescue Autism Child Abuse Protect Our Planet Autism Protect Our Planet
Originally Posted by Peter View Post
This should actually be giving out parse errors. replace $value['$key'] with $value[$key] and also replace $value[$key'] with $value[$key]
Okay, did that and still the same issue. Can send a blank form (it's not error checking) and when filling out all the checkboxes, just says Array in the output
NameCharger is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 09:53 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger