Render form in drupal separately - php

I want to render form in drupal separately, item by item.
function form($form){
$form['item1'] = array(
'type' => 'textarea', .........etc
$form['item1'] = array(
'type' => 'textarea', .........etc
$form['submit'] = array(
'type' => 'submit', .........etc
}
$a = drupal_get_form('form');
drupal_render($a['item1']);
drupal_render($a['item2']);
drupal_render($a['submit']);
form is rendered successfully, but it seems that submit button not work (not call submit function)
Any help?
Thanks

You just need to call drupal_render() one more time:
drupal_render($a);
It won't re-render everything you previously rendered.

Related

How to get a value from a form defined in one function, using a submit button defined in another function? - Drupal 7.x

I'm having problems displaying a textfield form value in drupal_set_message on clicking a form button which has been defined in another function in Drupal 7.x.
My First Function contains the Form textfield only (I don't want a 'Submit' button in the first function cause of the way I want to display the form UI separately on a page)
function firstformfun_form($form, &$form_state) {
$form['name'] = array(
'#title' => t(''),
'#type' => 'textfield',
'#attributes' =>array('placeholder' => t('test')),
);
return $form;
}
The Other function consisting of other forms and a 'Submit' button:
function firstformfuntwo_form($form, &$form_state) {
$form['name'] = array(
'#title' => t(''),
'#type' => 'textfield',
'#attributes' =>array('placeholder' => t('test')),
);
$form['submitbtn'] = array(
'#title' => t('Submit'),
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('submit_name'),
);
return $form;
}
I've defined a function for the 'Submit' button form as below and want it to fetch the value from the form 'Name' (textfield) in function 'firstformfun_form':
function submit_name($form, &$form_state) {
$output = $form_state['values']['name'];
drupal_set_message('Name ' .$output);
return $page;
}
The result of the same only displays 'Name' in a drupal message box, but doesn't display the output value from the 'Name' text box. Could someone kindly guide me on this?
Thanks

Submitting form to different actions in CakePHP 2.6 results in blackhole

I've got a form in my CakePHP 2.6 app which has multiple buttons to submit the form to different actions. With Cake 2.4, this worked, but on 2.6 submitting to anything other than the default form action leads to a blackhole 'auth' error.
Just before the end of my form, I have multiple submit buttons, like so:
echo $this->Form->button('Default', array(
'type' => 'submit',
));
echo $this->Form->button('Alternate 1', array(
'type' => 'submit',
'formaction' => '/posts/otheraction',
));
echo $this->Form->button('Alernate 2', array(
'type' => 'submit',
'formaction' => '/posts/anotheraction',
));
Reading the docs, I see ‘auth’ Indicates a form validation error, or a controller/action mismatch error.. However, this worked in the past- it seems like things have gotten stricter. How can you get Cake to accept submitted forms from other actions without turning off Security completely?
That doesn't seem to be possible (at least not without jumping through enourmously large hoops), as the action is being incorporated in the token unconditionally.
$hashParts = array(
$this->_lastAction, // <<<<<<<<<<<
serialize($fields),
$unlocked,
Configure::read('Security.salt')
);
$fields = Security::hash(implode('', $hashParts), 'sha1');
https://github.com/cakephp/cakephp/blob/2.7.1/lib/Cake/View/Helper/FormHelper.php#L589
Also disabling this behavior would possibly weaken security, as posting to actions the data wasn't ment to be used for, might cause unintended behaviour.
You can handle this with a single action though, for example by giving the various submit buttons a name and a value, and in your controller action evaluate the value and do whatever needs to be done
echo $this->Form->button('Default', array(
'type' => 'submit',
'name' => 'action',
'value' => 'default'
));
echo $this->Form->button('Alternate 1', array(
'type' => 'submit',
'name' => 'action',
'value' => 'alternate1'
));
echo $this->Form->button('Alernate 2', array(
'type' => 'submit',
'name' => 'action',
'value' => 'alternate2'
));
switch ($this->request->data('action')) {
case 'default':
// ...
break;
case 'alternate1':
// ...
break;
case 'alternate2':
// ...
break;
}
I used jQuery to change the form's action
HTML button:
<button id='saveSubmitter'>Save</button>
javascript:
$('body').on('click', '#saveSubmitter',
function(e){
e.preventDefault()
$('form').attr('action', '<?php echo Router::url( array("action"=>"save")); ?>')
$('form').submit()
}
)

Magento Varien_Data_form button value is always blank

I have a custom module, inside:
/Block/Adminhtml//Edit/Tab/Form.php
I am adding a bunch of fields which relate to fields in a table - all works fine and dandy.
However, I have a button on the page which executes some JavaScript, like so:
$fieldset->addField('trigger', 'submit', array(
'name' => 'trigger',
'label' => 'test',
'value' => Mage::helper('modulename')->__('Submit'),
'style' => 'width:100px;',
'onclick' => $this->getProductChooserURL(),
));
Now, for the life of me, I cannot set the value of that button - at the minute, it just shows as a blank html button - I'm just trying to get it to say something on it!
Someone else has had a similar problem here:
http://www.magentocommerce.com/boards/viewthread/283801/#t397177
But I can't make sense of the solution.
Hello if you want call javascript following code may be help you
$eventElemall=$fieldset->addField('trigger', 'submit', array(
'name' => 'trigger',
'label' => 'test',
'value' => Mage::helper('modulename')->__('Submit'),
'style' => 'width:100px;',
'onclick' => "getProductChooserURL()",
));
$eventElemall->setAfterElementHtml('
<script type="text/javascript">
function getProductChooserURL(){
document.getElementById("trigger").value }
</script>');

Yii framework,CGridView Checkbox help

I want an alert that pops when user tries to click "add to favorite"
1) if there's no checkboxes checked.
2) I also want to know how to get the values of the checked boxes here's my current code on backend
<?php $this->widget('zii.widgets.grid.CGridView',array(
'id' => 'wsrecruitcvhead-grid',
'dataProvider' => $model->search(),
#'filter' => $model,
'columns' => array(
array(
'name' =>'',
'value' => 'CHtml::checkBox("rid[]",null,array("value"=>$data->ResumeID,"id"=>"rid_".$data->ResumeID))',
'type'=>'raw',
'htmlOptions' => array('width'=>5),
'visible' => !Yii::app()->user->isGuest,
),
array(
'name' => 'ResumeTitle',
'value' =>$model->ResumeTitle,
),
'ResumeSummaryIntroduction',
'Name',
array(
'class' => 'CButtonColumn',
'viewButtonUrl' => 'Yii::app()->createUrl("wsrecruitcvhead/view",array("id"=>$data["ResumeID"]))',
'template'=>'{view}',
),
),
));
?>
and here's the screen shot http://pastebin.com/sEpJBCiU
I hope this helps you.
Your code works just fine, just tried it. I would implement the functionality you mentioned using jQuery, like this:
<script type="text/javascript">
$("input:checkbox").click(function () {
var thisCheck = $(this);
if (thisCheck.is (':checked')){
// do what you want here, the way to access the text is using the
// $(this) selector. The following code would output pop up message with
// the selected checkbox text
$(this).val());
}
});
</script>
This code selects all those DOM elements of type checkbox, and then checks with the if condition if it is selected or not. Here you can find more information about this jQuery selector. jQuery is such a powerful tool!!
For get all seleccions of a Grid yii provides the follow code
var seleccions = ($.fn.yiiGridView.getSelection('id_my_cgridview'));
this return an array with the values of checkboxes selected

Drupal Form API and $form_state['storage'] is destroyed on a page refresh

I have a form that displays two submit buttons. The first submit button sets $form_state['storage'] to a value. The second submit button then reads this $form_state['storage'] value. If the value is set, then a success message is displayed. If the value is not set, then a fail message is displayed.
Here is the code that will reproduce my issue:
function mymodule_test_admin() {
return drupal_get_form('mymodule_test_form');
}
function mymodule_test_form(&$form_state) {
$form['mymodule_test_form1'] = array(
'#type' => 'fieldset',
'#title' => t('test 1'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE
);
$form['mymodule_test_form1']['submit'] = array(
'#type' => 'submit',
'#value' => t('button 1'),
'#submit' => array('mymodule_test_form1_submit')
);
$form['mymodule_test_form2'] = array(
'#type' => 'fieldset',
'#title' => t('test 2'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE
);
$form['mymodule_test_form2']['submit'] = array(
'#type' => 'submit',
'#value' => t('button 2'),
'#submit' => array('mymodule_test_form2_submit')
);
return $form;
}
function mymodule_test_form1_submit($form, &$form_state) {
$form_state['storage']['test_1'] = 'test 1';
drupal_set_message(t('#message', array('#message' => $form_state['storage']['test_1'])));
}
function mymodule_test_form2_submit($form, &$form_state) {
if (isset($form_state['storage']['test_1'])) {
drupal_set_message(t('success'));
} else {
drupal_set_message(t('fail!'));
}
}
When you click the first submit button, $form_state['storage'] is properly set. When you click the second submit button, the message "success" is displayed. So far so good. Now do a page refresh. The message "fail!" is displayed.
So everything works right up until the page refresh. The page refresh essentially is only calling the second submit function. In theory, $form_state['storage'] should still be populated and the message displayed should be "success". However, taking a look at the $form_state dump shows $form_state['storage'] is NULL after the page refresh. I can't tell if my code logic is wrong or if $form_state['storage'] is being cleared on the page refresh.
Any ideas?
Thanks for your help.
You will need to rebuild form at the end of processing form1_submit, this will keep the previously submitted values intact. This is some what a scenario of multistep forms, but a bit different from the way it was done in Drupal 5.
function mymoduel_test_form1_submit($form, &$form_state) {
$form_state['storage']['test_1'] = 'test 1';
drupal_set_message(t('#message', array('#message' => $form_state['storage']['test_1'])));
$form_state["rebuild"] = TRUE;
}
Hope this helps,
Sarfaraz
Storage after submit will cleared, use $_SESSION['mymodule_test_XXX'] for storing in multistep forms...

Categories