#1 2013-05-29 11:55:57

Dzhalagash
Member
Registered: 2013-05-16
Posts: 25

Reading the data from multiple checkboxes.

I have this situation:
In a form a user must select one or more elements from a group of checkboxes.
F.e.
I like:
[]PHP []JS []Java []MySQL , select at least 1.
(just in case somebody was going to suggest it, a multiple select is not an acceptable solution)

Then the result must be processed and sent by email, the mail will show a list of the checked fields.
Echo 'The user liked'.$results;
The problem is about how to write that data into $results.

I'm actually able to send a mail with data from a form, but here is a bit trickier at reading the data.
Usually, if I want to ask for a name (mandatory field) and then send a mail with it:
\theme\view\contacts.php (the form)

<form class="form-horizontal" method="post" action="#contactForm">
<!-- The form name must be set so the tags identify it -->
<input type="hidden" name="form" value="contact" />
<!-- Input : Name -->
<div class="control-group<ion:form:contact:error:form_firstname is="true"> error</ion:form:contact:error:form_firstname>">
  <label class="control-label" for="form_firstname"><ion:lang key="form_label_form_firstname" />*</label>
    <div class="controls">
    <input class="span4" name="form_firstname" type="text" id="form_firstname" placeholder="<ion:lang key='form_label_form_firstname' />" value="<ion:form:contact:field:form_firstname />" />
    <ion:form:contact:error:form_firstname tag="span" class="help-inline" />
  </div>
</div>
<button type="submit" class="btn btn-success"><ion:lang key="button_send" /></button>

\theme\config\forms.php (form configuration)

'fields' => array
		(
			'form_firstname' => array
			(
				// CI validation rules
				'rules' => 'trim|required|min_length[3]|xss_clean',
				// Label translated index, as set in language/xx/form_lang.php
				// Will be used to display the label name in error messages
				'label' => 'form_label_form_firstname',
			)

\theme\language\xx\form_lang.php
for the various traductions, AND:
\theme\view\mail\form_contact\to_administrator.php (the mail template)

<ion:data:form_firstname /> // here I obtain the name
//eventually I can use traductions F.e. <ion:data:lang key="form_translation(that will have a %s field)" swap="data::form_firstname"/>

However in my case I need first to ask for the 4 checkbox:

<div class="control-group<ion:form:contact:error:form_interest is="true"> error</ion:form:contact:error:form_interest>">
<div class="row">
  <label class="span2" for="form_interest"><ion:lang key="form_label_form_interest" /></label>
  <div class="span2">
    <label class="checkbox" for="form_interest">
      <input type="checkbox" name="form_interest" value="<ion:lang key="form_label_PHP" />"><ion:lang key="form_label_PHP" />
    </label>
    <label class="checkbox" for="form_interest">
      <input type="checkbox" name="form_interest" value="<ion:lang key="form_label_JS" />"><ion:lang key="form_label_JS" />
    </label>
    <label class="checkbox" for="form_interest">
      <input type="checkbox" name="form_interest" value="<ion:lang key="form_label_Java" />"><ion:lang key="form_label_Java" />
    </label>
    <label class="checkbox" for="form_interest">
      <input type="checkbox" name="form_interest" value="<ion:lang key="form_label_MySQL" />"><ion:lang key="form_label_MySQL" />
    </label>
  </div>
</div>
<ion:form:contact:error:form_interest is="true"><span class="help-inline"><ion:lang key="form_alert_form_interest" /></span></ion:form:contact:error:form_interest>
</div>

Then I need to check it to make sure that one or more is selected:
(and this does work as intended, if 1 or more is selected there is no error client side, otherwise there is)

'form_interest' => array(
                'rules' => 'required',
                'label' => 'form_label_form_interest',
            )

Last I need to send it in the mail...

no clue,
$result = '<ion:data:form_interest />'; only gives me the last selected field

I tried converting "form_interest" fields into "form_interest[]" fields,
also in \theme\config\forms.php

'form_interest[]' => array(
                'rules' => 'required',
                'label' => 'form_label_form_interest',
            )

but here obviously I get errors...
I tried mix of the above, but nothing..

Last edited by Dzhalagash (2013-05-29 11:58:38)

Offline

#2 2013-05-29 12:42:57

GWR
Member
Registered: 2013-02-25
Posts: 125

Re: Reading the data from multiple checkboxes.

Instead of

    <label class="checkbox" for="form_interest">
      <input type="checkbox" name="form_interest" value="<ion:lang key="form_label_MySQL" />"><ion:lang key="form_label_MySQL" />
    </label>

try:

    <label class="checkbox" for="form_interest">
      <input type="checkbox" name="form_interest[]" value="<ion:lang key="form_label_MySQL" />"><ion:lang key="form_label_MySQL" />
    </label>

So: add [...] brackets after the name of the input, else you overwrite the prior ones.


bye
Ron

Offline

#3 2013-05-29 13:25:41

Dzhalagash
Member
Registered: 2013-05-16
Posts: 25

Re: Reading the data from multiple checkboxes.

Hi Ron, thanks for your time,
With this configuration I receive no error client side, but in the mail instead of the field "PHP"(for example) i receive:
"Tag missing: form_interest, scope: data."
or:

<p><span style=3D"color:#b00;font-=
weight:bold;">Tag missing:</span> <b>form_interest</b>, scope: <b>data</b>.=
</p>

In the mail view that field inside a php block looks like this:

$_interest = '<ion:data:form_interest />';
echo $_interest;

There is no iteration to read data:form_interest as an array containing a list of datas, I don't know how I should do it actually...
BTW I'm testing this in 0.9.9.5, in the 1.0 migration I'm unable so far to send mail at all (though the theme folder is the same).

Last edited by Dzhalagash (2013-05-29 13:29:47)

Offline

#4 2013-05-30 13:03:10

GWR
Member
Registered: 2013-02-25
Posts: 125

Re: Reading the data from multiple checkboxes.

You will have to have a look at the ion-data-functions. Trace the variables it uses. I think the current system may be not aware of fields[].


bye
Ron

Offline

#5 2016-04-08 16:33:04

fredericomarinho.com
Member
From: Juiz de Fora / Brazil
Registered: 2015-06-08
Posts: 40
Website

Re: Reading the data from multiple checkboxes.

With the same problem here. Somebody did it?

Last edited by fredericomarinho.com (2016-04-08 16:33:20)


Frederico Marinho - Social Media and Web Developer
contato@fredericomarinho.com
fredericomarinho.com

Offline

What's Ionize ?

Ionize is an Open Source Content Management System created by webdesigners for webdesigners.

Created and maintained by Partikule and Toopixel, Ionize wants to be the easiest and most powerful CMS.

Can I help ?

Because talent is nothing without involvement, we are looking for motivated coders and webdesigners to join the project team.

Resources

Website : ionizecms.com
Documentation : doc.ionizecms.com

Development : Ionize on GitHub
Translations : Ionize Translations