Bill Simpson
Bill Simpson

Reputation: 1

How can I get this form submit to work properly?

The following code is what I am using to e-mail me the contents of a form. I am having two problems with it and was hoping someone could help.

  1. I have checkboxes on the form, and when multiple boxes of the same group are checked, the e-mail I receive only shows the last box that was checked and not all that were checked.
  2. I have a <textarea> on the form and it doesn't show up in the e-mail sent to me either.

The PHP code:

<?php

header('Location: thank-you.html'); 

$from = $_POST['email'];
$to = '[email protected]';
$subject = "subject";

$message = "";
foreach ($_POST as $k=>$v)
{
    if (!empty($message)) { $message .= "\n\n"; }
    $message .= ucwords(str_replace('_',' ',$k)).": ".$v;
}

$headers = "From: $from";

mail($to, $subject, $message, $headers);

?>

Upvotes: 0

Views: 69

Answers (1)

codercake
codercake

Reputation: 1017

you probably have checkbox groups like this:

<input type=checkbox name=box value='one'>
<input type=checkbox name=box value='two'>

when they should look like this (with square brackets after the name)

<input type=checkbox name=box[] value='one'>
<input type=checkbox name=box[] value='two'>

php will then store the values in an array in $_POST['box'], which you can then join into comma delimited string inside your existing print code somewere with implode(', ', $_POST['box']) for formatting.

Upvotes: 1

Related Questions