0

我正在努力通过表单发送多个复选框条目。

我正在使用 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()));
  }

}
4

2 回答 2

0

将每个复选框的名称更改为唯一名称或“需要 []”。使用 "needs[]" 将传输一个数组中的所有复选框 $_POST["needs"]

于 2013-08-27T23:01:18.480 回答
0

您是否将其发布到另一个 PHP 脚本?每个复选框都有名称“需要”;尝试将它们更改为“needs-cms”、“needs-ux”之类的内容,以便您可以使用 $_POST['needs-cms']、$_POST['needs-ux'] 访问它们的值。我想在这种情况下它会是$form->htmlValue('needs-cms'), $form->htmlValue('needs-ux'),等等。

于 2013-08-27T22:33:17.057 回答