Unstoppable Domains

Php form process multiple checkboxes

Spaceship Spaceship
Watch
Impact
24
Hello,

Trying to process a form with 2 sets of checkboxes and sending results in email but getting stumped.

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:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
$roomsFlooring = $_POST['$roomsFlooring'] = implode(', ', $_POST['$roomsFlooring']);


See the problem? =)
 
0
•••
$roomsFlooring = $_POST['$roomsFlooring'] = implode(', ', $_POST['$roomsFlooring']);


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.
 
0
•••
Untested, and off the top of my head, but maybe something like this:

PHP:
<?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))
            {
                $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)
    {
        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.';
    }
}

?>
 
0
•••
$value["$key"]


bit of topic but why are you using the double quotes when the only thing you are entering is the variable?
 
0
•••
Thanks for the info.

Here's what I have now:
PHP:
/* 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)));
}

/* 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))
    {
        header('Location: thankyou.php');
        exit;
    }
    else
    {
        echo 'Sorry, mail 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.
 
0
•••
Thanks for the info.

Here's what I have now:
PHP:
/* 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)));
}

/* 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))
    {
        header('Location: thankyou.php');
        exit;
    }
    else
    {
        echo 'Sorry, mail 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]
 
0
•••
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 :|
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back