我很难通过表单发送多个复选框条目。
我正在使用kirby CMS和联系人表单插件。
这是我的代码,如果有人能对此有所了解的话。
<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>
我相信上面的代码是正确的,可能与提交文件有关。
<?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()));
}
}
您是否将其发布到另一个PHP脚本?每个复选框都有名称"needs";尝试将它们更改为"needs cms"、"needs ux",这样您就可以使用$_POST['needs-cms']、$_POST['needs-ux']访问它们的值。我想在这种情况下应该是$form->htmlValue('needs-cms'), $form->htmlValue('needs-ux')
等。
将每个复选框的名称更改为唯一名称或更改为"needs[]"。使用"needs[]"将转移一个数组$_POST["needs"]中的所有复选框