Checkboxes not showing for Drupal form - php

I am trying to get a Drupal form to show checkboxes, but I seem to be having no luck.
function multi_reg_pagecreate() {
$multi_reg_checkbox = multi_reg_checkbox();
print_r($multi_reg_checkbox);
$form['multi_reg_checkbox'] = array(
'#type' => 'checkboxes',
'#options' => $multi_reg_checkbox,
'#description' => t('Register for multiple events here')
);
return $form;
}
The data is in this format:
Array ( [22] => Test Event Reg [23] => Test Event Reg 2 )
Which seems similar to what is said in the example:
https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/6#checkbox
What am I not doing right?
EDIT:
I'm seeing this on the page displayed:
<form id="multi-reg-pagecreate" accept-charset="UTF-8" method="post" action="/domain/multiple-registration">
<div>
<div class="form-item form-type-checkboxes form-item-multi-reg-checkbox">
<label for="edit-multi-reg-checkbox">Events </label>
<div id="edit-multi-reg-checkbox" class="form-checkboxes"></div>
<div class="description">Register for multiple events here</div>
</div>
<input type="hidden" value="form-t_KSPV9ULp71yMEUHCtDUKV4R3M18M4ie2_M6cj-ZVU" name="form_build_id">
<input type="hidden" value="RzzEFjyCz29CjH9F9PAB5UV9Xq9VBCx8mTY_HppLfiA" name="form_token">
<input type="hidden" value="multi_reg_pagecreate" name="form_id">
</div>
</form>

function multi_reg_pagecreate($form, &$form_state) {
$multi_reg_checkbox = multi_reg_checkbox();
$form['multi_reg_checkbox'] = array(
'#title' => t('Events'),
'#type' => 'checkboxes',
'#options' => $multi_reg_checkbox,
'#description' => t('Register for multiple events here')
);
return $form;
}
EDIT:
Options has to look like:
'#options' => array (
0 => t('Monday'),
1 => t('Tuesday'),
2 => t('Wednesday'),
3 => t('Thursday'),
4 => t('Friday'),
5 => t('Saturday'),
6 => t('Sunday'),
),
or:
'#options' => drupal_map_assoc(
array(0,1,2,3,4,5,6)
),
... looks like:
Please post your screen from your current devel output of $multi_reg_checkbox

I know this question is old... but... how are you calling the form? Are you doing it from a hook_menu? If so: did you make sure to put
'page callback' => 'drupal_get_form',
'page arguments' => array('multi_reg_pagecreate'),

Related

posted value is not displaying when use enctype as multipart/form-data in cakephp

I am working in cakephp.I have created one form that include file upload and textbox nad textarea.
Here, my html code looks like below :
<form action="" id="frmReg" method="post" enctype= "multipart/form-data">
<div style="position:relative; margin-bottom:30px;" class="fildtpart4">
<label><?php __d('statictext', 'Question', false); ?>:</label>
<span>
<textarea class="validate[required]" name="question1['question']" cols="" rows=""></textarea>
<div style="color: red; position:absolute ; bottom:-25px ;" id="charNum"></div>
</span>
<div class="clear"></div>
<div class="fildtpart3">
<label><?php __d('statictext', 'Currect Answer', false); ?></label>
<span>
<input name="question1['currect_ans']" type="text" class="validate[required]" id="" value="">
<input type="file" class="validate[required]" onchange="showMyImage1(this)" name="question1['sponsor_image']" id="" />
<input type="file" class="validate[required]" onchange="showMyImage2(this)" name="question1['question_image2']" id="" />
</span>
<input type="submit" value="submit" name="submit">
<div class="clear10"></div>
</div>
</div>
<form>
when I submit this form then it will shows only image not all data.
In my controller, I have written :
function add_polls()
{
print_r($this->params['form']);exit;
}
Then it gives output like :
array(
[question1]=>array(
[name] => Array
(
['sponsor_image'] => contact.jpg
['question_image2'] => contact.jpg
)
[type] => Array
(
['sponsor_image'] => image/jpeg
['question_image2'] => image/jpeg
)
[tmp_name] => Array
(
['sponsor_image'] => /tmp/phpUK7Vcj
['question_image2'] => /tmp/php3SCWGZ
)
[error] => Array
(
['sponsor_image'] => 0
['question_image2'] => 0
)
[size] => Array
(
['sponsor_image'] => 2305
['question_image2'] => 2305
)
)
)
Here, its not printed question1['currect_ans'] and question1['question'].When I remove enctype from the form then it will shows all value. So how can I resolve this problem?
Note: CakePHP vesrion is 1.3.13.
This is a mixture of how PHP handles files, and you not following the CakePHP conventions, causing CakePHP to "overwrite" your other form data.
You should use the form helper, that way you would avoid the problem in the first place, unless of course you'd fiddle with the field names and use such clashing ones again.
Adding everything in question1, including files, will result in the files being grouped under question1 in $_FILES, just as shown in your question, ie
Array(
[question1] => Array(
[name] => Array(
['sponsor_image'] => contact.jpg
['question_image2'] => contact.jpg
)
// ...
)
)
and the rest of the form data in $_POST using the same key, ie
Array(
[question1] => Array(
['question'] => 'foo',
['currect_ans'] => 'bar'
)
)
You should already smell the upcoming clash.
CakePHP will first fill $params['form'] with $_POST, and then iterate over $_FILES and use the keys to set the file data in $params['form'] too, which will lead to the previously set form data, ie the text inputs, to be overwritten, as both use the key, question1.
https://github.com/cakephp/cakephp/blob/1.3.13/cake/dispatcher.php#L248
https://github.com/cakephp/cakephp/blob/1.3.13/cake/dispatcher.php#L289-L293
tl;dr Follow the naming conventions, or use the form helper
Follow the CakePHP conventions and use the data key (and optionally a model name, which doesn't need to exist) to group the input, that way all data will be properly handled separately and made available via Controller::$data
name="data[question]"
name="data[currect_ans]"
name="data[sponsor_image]"
name="data[question_image2]"
No need to use quotes for nested keys btw.
Even better, as already mentioned, use the form helper, it will automatically create proper fieldnames.
See also
Cookbook > Core Helpers > Form > Field naming convention
Cookbook > Core Helpers > Form > File Fields
So i reproduced your issue and i'm reporting the following.
On my cakephp 3.x your form works great by submitting with 'regular' HTTP post. (I added an <input type="submit"> to your form).
[
'question1' => [
'question' => 'da',
'currect_ans' => 'da',
'sponsor_image' => [
'name' => 'Mobile version.png',
'type' => 'image/png',
'tmp_name' => '/tmp/php3A021O',
'error' => (int) 0,
'size' => (int) 149447
],
'question_image2' => [
'name' => 'Mobile version.png',
'type' => 'image/png',
'tmp_name' => '/tmp/phpi6VWxl',
'error' => (int) 0,
'size' => (int) 149447
]
]
]
So how are you submitting your data? Is it through ajax? What version of cakephp are you using? Try answering these questions so i can help you further on.
Have you tried getting the data using
$postedData = $this->request->data;

Need simple php help, but I don't really know php yet

<div class='rate-results'>
<div class='button-container'>
<input type='submit' id='btn-calculate' class='pure-button pure-button-primary' value='{$a['label_button_continue']}'></input>
</div><div class='rate-container'>
<div class='label_calculated_rate'>{$a['label_calculated_rate']}</div>
<div id='calculated_rate'>{$a['default_price']}</div>
</div>
how can i apply a link to this button?
['label_calculated_rate']
seems to pull the text
"GET A QUOTE"
from code up above it, but nowhere can i find how to turn this button into a link. The mentioned above code is as follows...
static function handle_shortcode($atts){
self::$add_script = true;
self::$add_styles = true;
$a = shortcode_atts( array(
'title' => 'Get a FREE Price Estimate',
'label_days' => 'Total days of coverage needed?',
'units_days' => 'days',
'label_attendance' => 'Estimated daily attendance?',
'units_attendance' => 'people',
'label_event_type' => 'What type of event is it?',
'label_sample_certificate' => 'View sample certificate',
'link_sample_certificate' => '#',
'label_button_continue' => 'Get Free Quote',
'label_calculated_rate' => 'Estimated Cost',
'default_price' => '$ 95.95',
'form_action' => 'javascript:void(0);',
'default_event_type' => ""
), $atts);
return "<form action='{$a['form_action']}' id='main_calculator' class='pure-form pure-form-aligned' data-default-event-type='{$a['default_event_type']}'>
<h3 class='title'>{$a['title']}</h3>
Replace
<div class='label_calculated_rate'>{$a['label_calculated_rate']}</div>
with
<div class='label_calculated_rate'>{$a['label_calculated_rate']}</div>
Replace http://www.w3schools.com/html/ with where you want the button to link to.
Hence adding a link to the button:
['label_calculated_rate']

cakephp generates wrong label for radio input id

When i create a form on CakePHP with radio inputs, the label generated not match with the id of the radio input, the label "for" duplicates the name of the form. Here is my code:
echo $this->Form->create(
'test',
array(
'action' => 'index',
'type' => 'post',
'class' => 'fill-up',
'inputDefaults' => array('div' => 'input')));
$options = array('option1' => '1',
'option2' => '2');
$attributes = array('legend' = > false);
echo $this->Form->radio('Type', $options, $attributes);
echo $this->Form->end(
array(
'label' = > 'end',
'class' = > 'button',
'div' = > false));
and the generated HTML is something like:
<input type="hidden" name="data[test][options]" id="testOptions_" value="">
<input type="radio" name="data[test][options]" id="TestOptionsOption1" value="option1">
<label for="testTestOptionsOption1">1</label>
<input type="radio" name="data[test][options]" id="TestOptionsOption2" value="option2">
<label for="testTestOptionsOption2">2</label>
as you can see, cake duplicate the form name "test" on the label. How I can fix this? I try with the exact code of the documentations and still have the same issue
hope you can help me, thx very much
I auto-answer my question. That was a bug of cakephp, solved on the last version:
https://github.com/cakephp/cakephp/releases/tag/2.4.2
Try using
'label' => array(
'class' => 'thingy',
'text' => 'The User Alias'
)

Cakephp - Checkbox names

Currently, i have succeeded in creating the checkbox.
The array which i have setup is as below:
$emailName = $this->User->find('list', array(
'fields' => array('User.username', 'User.email')
));
The output is as follows:
array
'admin' => string 'asd#asd.asd' (length=11)
'test' => string 'test#test.test' (length=14)
'Floo' => string 'XXXX#gmail.com' (length=16)
I'm trying to make the checkbox shows the username instead of the user email in view.ctp.
I have tried using the following code in view.ctp
<?php echo $this->Form->input('Address_list.['.$emailName['username'].']', array(
'type' => 'select',
'multiple' => 'checkbox',
'options' => $emailName['email']
)); ?>
However, it seems that this doesn't work. Any ideas?
You are not formatting your form field for the checkbox list correctly. Try changing it to this:
echo $this->Form->input('Address_list', array(
'multiple' => 'checkbox',
'options' => $emailName,
));
However, this will return the value of Username based on the selection of Email the user chooses. It creates the form like this:
<label for="Address_list">Address List</label>
<input type="hidden" id="Address_list" value="" name="data[Address_list]"/>
<div class="checkbox"><input type="checkbox" id="AddressListAdmin" value="admin"
name="data[Address_list][]"/><label for="AddressListAdmin">asd#example.com</label></div>
<div class="checkbox"><input type="checkbox" id="AddressListTest" value="test"
name="data[Address_list][]"/><label for="AddressListTest">test#example.com</label></div>
<div class="checkbox"><input type="checkbox" id="AddressListFloo" value="Floo"
name="data[Address_list][]"/><label for="AddressListFloo">XXX#example.com</label></div>

Why drupal_render($form) does not render correctly RADIOS #type?

I'm trying to use drupal_render() to render a single form element. I can successfully render elements of '#type' => 'textfield' or 'radio' or 'whatever'.
When I try to render an element of '#type' => 'radios' something goes wrong. I can't find out why but the radios simple won't show.
$options = array(
'0' => 'no option',
'1' => 'option 1',
'2' => 'option 2',
'3' => 'option 3',
'4' => 'option 4',
'5' => 'option 5'
);
$form['radiosinput'] = array(
'#type' => 'radios',
'#title' => 'radios title',
'#description' => 'radios description',
'#default_value' => 0,
'#options' => $options,
'#required' => TRUE,
);
var_dump( drupal_render($form) );
// string(257) "<div class="form-item">
// <label>radios title: <span class="form-required" title="This field is required.">*</span></label>
// <div class="form-radios"></div>
// <div class="description">radios description</div>
// </div>
// "
Anyone knows what's the problem and the fix/workaround?
Is there any known problem with rendering radios or something?
Thanks!
You can't render form elements without a form because the the radios element has a process callback of form_process_radios() that is called only when used with the form API.
You might be able to try something like:
$form['radiosinput'] = expand_radios($form['radiosinput']);
return drupal_render($form);
For D7 use form_process_checkboxes()
I was working on something similar an hour ago. Your code pasted into my form works just fine.
Try
drupal_get_form('your_form_id');
Does that work?
I had the same trouble with the checkboxes element I wanted to render in a table. I found your answer a little bit to late.
So for all other searching how to render a checkboxes element with drupal_render and expand_checkboxes:
$form['test'] = array(
'#type' => 'checkboxes',
'#title' => t('Test'),
'#description' => t('The description appears usually below the checkboxes.'),
'#options' => array(1,2,3,4),
);
drupal_render(expand_checkboxes($form['test']));
I might add, that in D6 you need to add #parents => array() to the radios element.
if no, expand_radios will throw an error, in my case. Drupal 6.22

Categories