Php Chheckbox表格提交
我很难通过表单发送多个复选框条目 我正在使用kirby CMS和联系人表单插件 这是我的代码,如果有人能解释的话Php Chheckbox表格提交,php,forms,content-management-system,kirby,Php,Forms,Content Management System,Kirby,我很难通过表单发送多个复选框条目 我正在使用kirby CMS和联系人表单插件 这是我的代码,如果有人能解释的话 <fieldset name="check"> <h2>Project Needs</h2> <ul> <li><input id="strategy" type="checkbox" name="needs" value="<?php echo $form->ht
<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脚本?每个复选框都有“需要”的名称;尝试将它们更改为“需要cms”、“需要ux”,以便您可以使用$\u POST['needs-cms']、$\u POST['needs-ux']访问它们的值。我猜在这种情况下应该是
$form->htmlValue('needs-cms'),$form->htmlValue('needs-ux')
,等等。将每个复选框的名称更改为唯一名称或“needs[]”。
使用“needs[]”将在一个数组中传输所有复选框$\u POST[“needs”]我尝试将复选框name=“needs”更改为name=“needs[]”,并尝试更改为我仅通过说出数组来接收电子邮件:(我需要保留所有被调用的名称,因为我希望返回以下结果:ux重新设计响应等
<?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()));
}
}