I'm struggling to send multiple checkbox entries through a form.
I'm using kirby CMS and the contact form plugin.
This is my code if anyone can shed any light on this.
<fieldset name="check">
<h2>Project Needs</h2>
<ul>
<li><input id="strategy" type="checkbox" name="needs" value="<?php echo $form->htmlValue('needs') ?> Strategy" checked><label for="strategy">Strategy</label></li>
<li><input id="ux" type="checkbox" name="needs" value="<?php echo $form->htmlValue('needs') ?> UX"><label for="ux">UX</label></li>
<li><input id="redesign" type="checkbox" name="needs" value="<?php echo $form->htmlValue('needs') ?> Re-Design"><label for="redesign">Re-Design</label></li>
<li><input id="responsive" type="checkbox" name="needs" value="<?php echo $form->htmlValue('needs') ?> Responsive Design"><label for="responsive">Responsive Design</label></li>
<li><input id="cms" type="checkbox" name="needs" value="<?php echo $form->htmlValue('needs') ?> CMS Implementation"><label for="cms">CMS Implementation</label></li>
<li><input id="unsure" type="checkbox" name="needs" value="<?php echo $form->htmlValue('needs') ?> Unsure"><label for="unsure">Unsure</label></li>
</ul>
</fieldset>
I believe the code above is correct and might be something to do with the submission files.
<?php
if(!class_exists('Submission')) require_once('lib/submission.php');
if(!class_exists('Email')) require_once('lib/email.php');
class ContactForm extends Submission {
public function __construct($params = array()) {
$this->defaults();
// set required and wanted fields
$this->defaults('required', array('name', 'email', 'phone', 'project', 'description', 'budget', 'needs', 'date'));
$this->defaults('keep', array('name', 'email', 'phone', 'project', 'description', 'budget', 'needs', 'date'));
// set the default subject
$this->defaults('subject', 'Oi, Gaylord.');
// take the current URL as the default goto URL
$this->defaults('goto', $_SERVER['REQUEST_URI']);
// set a custom validation event
$this->defaults('validate', function($self) {
// validate the email address
if(!filter_var($self->value('email'), FILTER_VALIDATE_EMAIL)) $self->addInvalid('email');
});
// try to send the email
$this->defaults('submit', function($self) {
$to = $self->option('to');
$from = $self->option('from');
if(!$from) $self->option('from', $to);
// set the email body
$self->option('body', $self->body());
// send the email form, pass all options
$send = email($self->options);
if(error($send)) {
$self->addInvalid('send');
return $self->trigger('error');
}
$self->trigger('success');
});
// redirect to the "goto" url on success
$this->defaults('success', function($self) {
// redirect to callback url
go($self->option('goto'));
});
// merge the defaults with the given options
$this->options($params);
// trigger the request
$this->trigger('request');
}
function body() {
$body = $this->option('body');
if(empty($body)) {
$body = snippet('contact.mail', array(), true);
if(empty($body)) {
$body = 'Name: {name}' . PHP_EOL;
$body .= '----' . PHP_EOL;
$body .= 'Email: {email}' . PHP_EOL;
$body .= '----' . PHP_EOL;
$body .= 'phone: {phone}' . PHP_EOL;
$body .= '----' . PHP_EOL;
$body .= 'project: {project}' . PHP_EOL;
$body .= '----' . PHP_EOL;
$body .= 'description: {description}' . PHP_EOL;
$body .= '----' . PHP_EOL;
$body .= 'budget: {budget}' . PHP_EOL;
$body .= '----' . PHP_EOL;
$body .= 'needs: {needs{}' . PHP_EOL;
$body .= '----' . PHP_EOL;
$body .= 'date: {date}' . PHP_EOL;
}
}
foreach($this->data() as $key => $value) {
$body = str_replace('{' . $key . '}', $value, $body);
}
return trim($body);
}
function htmlBody() {
return nl2br(html($this->body()));
}
}
Are you POSTing this to another PHP script? Every checkbox has the name "needs"; try changing them to something like "needs-cms", "needs-ux", so that you can access their values with $_POST['needs-cms'], $_POST['needs-ux']. I guess in this case it'd be
$form->htmlValue('needs-cms'), $form->htmlValue('needs-ux')
, etc.