Cakephp onChange event on select form (dropdown) - javascript

I have a form with select input. I want to auto submit the form when the dropdown list was selected.
My code:
<?php echo $this->Form->create('Product', array('controller'=>'products', 'action'=>'shipping_area'));
echo $this->Form->input('area', array('options' => array('1' => 'Within Malaysia', '2' => 'International'), 'empty' => 'choose area',
'label' => 'Choose shipping area', 'onChange'=>'javascript:this.form.submit()'));
//echo $this->Form->end('Save');
?>
I put 'onChange'=>'javascript:this.form.submit()', but it goes to http://localhost/cake/cake/products/shipping_area ( supposely http://localhost/cake/products/shipping_area )
I also tried 'onChange'=>'this.form.submit()', but got same error.
can anyone please help.

You can add an "id" attribute to the form and then every time that you get an "onchange" event in the "select" element, you have to obtain the "id" value and pass it to the javascript function "document.getElement('idValueHere')" and call to the fuction submit. More clearly, the following code:
<?php
# step 1: add an id attribute ('id' => 'anyFormName') to the array options
# step 2: add an onchange envent to the dropdown ('onChange'=>'document.getElementById("anyFormName").submit();')
echo $this->Form->create('Product', array('controller'=>'products', 'action'=>'shipping_area', 'id' => 'anyFormName'));
echo $this->Form->input('area', array('options' => array('1' => 'Within Malaysia', '2' => 'International'), 'empty' => 'choose area',
'label' => 'Choose shipping area', 'onChange'=>'document.getElementById("anyFormName").submit();'));
//echo $this->Form->end('Save');?>

Hope it helps you.
echo $this->Form->create('Product', array(
'url' => array(
'controller'=>'products', 'action'=>'shipping_area'
)
));

Related

I'm using YII2, I'm displaying a list of inactive users. I'm using the checkbox to select the user. I'm going to send an email to the user I tagged

$user
id
name
001
user 1
002
User 2
<ul>
<?php foreach($user as $user( { ?>
<input class="is-selected" type="checkbox" value="<?= $value->user->id ?>" />
<?php } ?>
</ul>
<button type="submit" class="btn is-primary">Send Reminder</button>
I'm using YII2, I'm displaying a list of inactive users. I'm using the checkbox to select the user. I'm going to send an email to the user I tagged.
how can i get id value from that checkbox and send data to controller?
I try to use javascript/jquery but i can't solved yet, i need some help from you guys
The above screenshot and your implementation is like you are talking 2 different things.
You would just be required a Grid View with CheckboxColumn
<?php
use yii\bootstrap\ActiveForm;
use yii\grid\GridView;
use yii\helpers\Html;
$form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
'id' => 'user_form',
'options' => ['class' => 'lfp-listing']
]);
echo Html::submitButton('Send Reminder', ['class' => 'btn is-primary', 'name' => 'action', 'value' => 'send_reminder']);
echo GridView::widget([
'dataProvider' => $dataprovider,
'columns' => [
'id',
'name',
'email',
[
'class' => 'yii\grid\CheckboxColumn',
'checkboxOptions' =>
function ($model) {
return ['value' => $model->id, 'class' => '', 'id' => 'checkbox'];
}
],
]
]);
ActiveForm::end();
Considering your Action is actionIndex()
public function actionIndex()
{
if (isset($_REQUEST['action']) && isset($_REQUEST['selection']) && 'send_reminder' == $_REQUEST['action'] ) {
foreach ($_REQUEST['selection'] as $userIds) {
//write down the email code here....
}
}

Cake PHP change value of hidden form item by javascript

I have the below cake php form in my view:
<?php $form_id = 'message_form ' + $profile_id ?>
<?php echo $this->Form->create('Post', array('id' => $form_id, 'url' => array('app' => true, 'controller' => 'messages', 'action' => 'new', $profile_id))); ?>
<?php echo $this->Form->input('text', array('type' => 'textarea', 'class' => 'form-control', 'label' => false, 'data-validate' => 'not-empty', 'name' => 'data[Message][content]', 'placeholder' => 'Reply...')); ?>
<?php echo $this->Form->input('hidden', array('type' => 'hidden', 'class' => 'form-control', 'label' => false, 'name' => 'data[Message][parent_id]', 'value' => 1)); ?>
<div class="message-reply-options">
<i class="fa fa-camera"></i>
<i class="fa fa-paperclip"></i>
<i class="fa fa-trash"></i>
<span class="pull-right">
<?php echo $this->Form->submit('Send', array('class' => 'form-control')); ?>
</span>
</div>
<?php echo $this->Form->end(); ?>
This works fine, however I need to be able to change the hidden fields value via javascript depending on something the user does else where on the page.
Below is my current javascript I am using for this, however it does not change the value (but gives no error either).
var newestmessage = response.ChildMessage[response.ChildMessage.length-1];
var parentid = newestmessage.parent_message_id;
document.getElementsByName('data[Message][parent_id]').value = $parentid;
This would have probably been a comment, but since I don't have enough reputation...
I guess you are trying to change/assign the value to hidden field on a particular, action.
I just assume a click event.
Jquery required
$('.classNameOfClickableObjt').clilck(function(){
$('input[name="data[Message][parent_id]"]').val(parentid);
/*Another alternative, you might assign attribute, like
$('input[name="data[Message][parent_id]"]').attr('value', parentid); */
})
You can try jquery and ajax functionally to check the value of hidden input box.

Adding inline JavaScript to form input element in CakePHP

I'm trying to do this:
echo $this->Form->input("filter.wijk", array(
'onChange' => 'this.form.submit()',
'type' => 'select',
'multiple' => 'checkbox',
'options' => $wijkOpties,
'label' => false)
);
However, the onChange attribute does not appear in the resulting HTML. The documentation for FormHelper::input tells us this:
* ### Options
*
* See each field type method for more information. Any options that are part of
* $attributes or $options for the different **type** methods can be included in `$options` for input().i
* Additionally, any unknown keys that are not in the list below, or part of the selected type's options
* will be treated as a regular html attribute for the generated input.
Am I interpreting that last sentence in the wrong way?
You have 2 options:
1- Give up on the checkboxes and simply get a normal select list element/box:
echo $this->Form->input("filter.wijk", array(
'onChange' => 'this.form.submit()',
'type' => 'select',
'multiple' => true,
'options' => $wijkOpties,
'label' => false)
);
2- Do a workaround for a valid checkboxes list (Something like):
$i = 0;
foreach($wijkOpties as $value => $option){
echo $this->Form->input('filter.wijk.' . $i++, array(
'onChange' => 'this.form.submit()',
'label' => $option,
'value' => $value,
'type' => 'checkbox',
));
}
`

get all IDs of selected checkbox in Yii using javascript

I am trying to get the IDs of all the selected check boxes in yii using JAVASCRIPT. Now i am able to get only the first element ID. Can anyone please suggest the correct code to get all the check box ID.
My View:
<input type="button" value="Multiple Host Date Entries" onclick="act();" />
<div id="grid"></div>
<?php
//zii.widgets.grid.CGridView bootstrap.widgets.TbExtendedGridView
$obj=$this->widget('bootstrap.widgets.TbExtendedGridView', array(
'id'=>'host_grid',
'dataProvider'=>$dataProvider,
'type' => 'striped bordered',
//'filter' => $model,
//'type' => 'striped bordered condensed',
//'summaryText' => false,
////'afterAjaxUpdate'=>'\'changeTRColor()\'',
//'itemView'=>'_view',
'columns'=>array(
array(
'id' => 'selectedIds',
'class' => 'CCheckBoxColumn',
'selectableRows'=>2,
'value' => '$data->host_id',
'checkBoxHtmlOptions' => array('name' => 'idList[]'),
),
array( // display 'create_time' using an expression
'name'=>'host_name',
'value'=>'$data->host_name',
),
array(
'name'=>'host_serviceid',
'value'=>'$data->host_serviceid',
),
array(
'name'=>'status',
'value'=>'$data->status',
),
array(
'class'=>'CButtonColumn',
'template'=>'{edit_date}{update}{delete}',
'htmlOptions'=>array('width'=>'95px'),
'buttons' => array(
'update'=> array(
'label' => 'Update',
'imageUrl' => Yii::app()->baseUrl.'/images/icons/a.png',
),
'delete'=> array(
'label' => 'Delete',
'imageUrl' => Yii::app()->baseUrl.'/images/icons/d.png',
),
'edit_date' => array( //the name {reply} must be same
'label' => 'Add Date', // text label of the button
'url' => 'Yii::app()->createAbsoluteUrl("NimsoftHostsDetails/View", array("id"=>$data->host_id))', //Your URL According to your wish
'imageUrl' => Yii::app()->baseUrl.'/images/icons/m.png', // image URL of the button. If not set or false, a text link is used, The image must be 16X16 pixels
),
),)
),
))
;
?>
I must select some check boxes and click on multiple host date entries button to go to the specific controller.
My JavaScript:
function act()
{
var idList=$("input[type=checkbox]:checked").serializeArray();
var jsonStr = JSON.stringify(idList);
/* Object.keys(idList).forEach(function(key) {
console.log(key, idList[key]);
alert(idList[key]);
});*/
var a=$("input[type=checkbox]:checked").val();
alert(a);
if(idList!="")
{
if(confirm("Add Dates for multiple hosts?"))
{
var url='<?php echo $this->createUrl('Nimsoft/Date_all',array('idList'=>'val_idList')); ?>';
url=url.replace('val_idList',jsonStr);
//url=url.replace('val_idList',json_encode(idList));
//alert(url);
window.open(url);
/*$.post('Date_all',idList,function(response)
{
$.fn.yiiGridView.update("host_grid");
});*/
}
}
else
{
alert("Please Select atleast one host");
}
}
I need to pass the IDs to NIMSOFT controller so that I can have a for loop to process each one of those.
you can retrieve the checked column in client side (javasctipt):
var idArray = $(gridID).yiiGridView('getChecked', columnID);
// or
$.fn.yiiGridView.getSelection(gridID);
For all checked row ids we use this
var id = $.fn.yiiGridView.getChecked("your-grid-id", "selectedIds"); // array of seleted id's from grid

Cakephp Ajax, Using Js Helper to get value of a text field

guys this the code I am using to make a ajax request, its not the complete code just relevant stuff
<div class="container whiteFont">
<label>New Tag</label>
<?php echo $this->Form->input('tagValue'); ?>
<input id="tag-button" type="button" value="Add Tag"/>
<div id=tag></div>
<?php
//on button click sands request to controller and displays response data in chosen field
$this->Js->get('#tag-button')->event(
'click',
$this->Js->request(
array('controller' => 'tags', 'action' => 'add',(Here need help),$this->Form->value('Post.id')),
array(
'update' => '#tag',
'async' => true,
)
)
);
?>
Now the problem is, my actions takes in two parameters.
I can set the second one which is Post.id
but I am not able to get the value of text field which has id of 'tagValue'.
I have tried using just html input fields as well.
When I try to use Js->get here it doesnt work as well.
help me out please.
Thank you
$this->Js->get('#tag-button')->event(
'click',
$this->Js->request(
array('controller' => 'tags', 'action' => 'add',(Here need help),$this->Form->value('Post.id')),
array(
'update' => '#tag',
'async' => true,
'dataExpression'=>true,
'data'=> $this->Js->serializeForm(array(
'Post.id' => 1
))
use dataExpression and data to send parameters.
For more info: http://book.cakephp.org/2.0/en/views.html
Proper way to get all your form parameters passed:
$this->Js->get('#tag-button')->event(
'click',
$this->Js->request(
array('controller' => 'tags', 'action' => 'add',(Here need help),$this->Form->value('Post.id')),
array(
'update' => '#tag',
'async' => true,
'dataExpression'=>true,
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true
))
))

Categories

Resources