CakePHP form selection as parameter - php

I have the following code in my index.ctp view to create a form:
<?php
echo $this->Form->create(false,array('url' => array('controller' => 'admins', 'action' => 'edit_gallery')));
echo $this->Form->input('name', array('options' => $array,'empty' => 'Select a gallery'));
echo $this->Form->end(__('Submit', true));
?>
Thi codes creates a dropdown list of items, each one with an associated number as value.
In my admins_controller I have the edit_gallery action implemented exactly as it comes when you bake a project, only that I changed the typical edit to edit_gallery.
What I want is the following: the user selects one item from the list, then clicks 'Submit', and he's taken to the edit_gallery.ctp view, with a form to edit the information of that item in the database and update it. My problem is that, instead of doing this, what happens is that when the user clicks Submit, a new item is created in the database, and it doesn't even show the ctp view.
In general, my question would be: how can I get the selected option of the form in the landing page after the user clicks 'Submit'?
Edit
Ideally, what I would want is that, when the user clicks 'Submit', it would send a request like admins/edit_gallery/x where x would be the value associated to the selection made by the user, without sending any other data to the action. I don't know if that's possible.
Thank you!

The edit method that CakePHP bakes check by default if data is not null, in other words, if a form has been submitted, and then updates that record.
So, when you redirect to *edit_gallery* from a form, the data property is not null, therefore the reason for the new item creation in the database.
There are many ways to solve this. One of them is to remove that check from the *edit_gallery* method, create another method like *save_gallery*, and call that method from *edit_gallery.ctp*.
So the *edit_gallery.ctp* form would look something like:
<?php
echo $this->Form->create(false,array('url' => array('controller' => 'admins', 'action' => 'save_gallery')));
(your form info)
echo $this->Form->end(__('Submit', true));
?>

Related

How to load 2 models into a single view in Yii2

Problem
This problem is regarding Yii2 project which should show relevant Recipient list when click on the User Group name.
At the moment I have created all the databases, models, CRUD generations with gridviews & working perfectly. But the problem is when I click on the grid item it navigates to another view (the default way) as following pic which I want to load in a part of the same view.
User-groups (view -> index.php)
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//navigate to the relevent recipient list by click on the group name
'rowOptions' => function ($model, $key, $index, $grid) {
return [
'style' => "cursor: pointer",
'id' => $model['group_id'],
'onclick' => 'location.href="'.Yii::$app->urlManager->createUrl('recipient-list/recipients').'&scenario=RECIPIENTS&params="+(this.id);',
];
},
'columns' => [
'group_id',
'group_name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
I have given the rowOptions in the GridView so that the record can be clicked & navigate to the recipient list through the URL. Then that will be captured from the controller & filter the result according to group_id & render the view.
What I want
I want to dynamically load the recipient list when click on the left side group list.
I have tried
iFrames but it is not working properly. it works as a separate tab in the browser.
added many Yii navigation extensions like sideNavs by kartik for group list. But the problem was it haven't the ability to getting data from database. when I create a group it didn't show in the list. (It is good if the side nav was static)
So .. What I want to know is, is there are any way to do this with the controller or is there any Jquery code to support this...
any suggestions or references are warmly welcome.
you can use jQuery .load for this.
lets say your recipient list container div has a id of #recipient-list so all you have to do is add the following on your onclick event:
$( "#recipient-list" ).load("YOUR_URL"); return false;
// replace YOUR_URL with the url you are generating already
// and don't forget the return false part. it't there so that link doesn't redirect
When you click in the row, on click event
you will make an ajax request to retrieve the
data for the second grid (Recipient List)
on success or complete of ajax request
you will call $.pajax.reload to refresh
the data in second grid like this
$.pjax.reload({container:'#recipient-list'});

CakePHP does not auto-populate a field (that has no Model) after submission

The situation
I'm working on a large form which have been split into several tabs, both to group fields of a kind and make filling easier. The tabs are activated using JavaScript (jQuery) and the form is submitted whole (all tabs at once, even if there are blank fields).
I am trying to submit a hidden field along with the form to keep user's current tab. This way, when the form is submitted, data is saved and the user goes back to the very same tab it was before. This field is not attached to any Model, as it is just a helper.
The problem + what have I tried
The field is correctly populated by jquery as the user switch tabs, and it is submitted along with the form. This is what I did:
<?php
echo $this->Form->create('Briefing', array('enctype' => 'multipart/form-data'));
// This is my aux field, which is populated via jQuery as user switch tabs
echo $this->Form->hidden('tab-active', array('name' => 'tab-active', 'id' => 'tab-active'));
echo $this->Form->input('title');
echo $this->Form->submit('Save', array('class' => 'btn btn-success'));
echo $this->Form->end();
?>
Let's suppose the user submitted the form when it was at "Tab #4". When I debug($this->data); at my BriefingsController, this is what I get:
/app/Controller/BriefingsController.php (line 133)
array(
'tab-active' => 'tab-4',
'Briefing' => array(
'title' => 'My test title'
)
)
So my Controller receives the form data for both tab-active and Briefing.title fields, but when the form is loaded after submission, only the Model field is populated with the submitted data, and tab-active comes with an empty value. CakePHP does not auto populate the value for my non-model-attached field.
Any thoughts on that? Is this a default CakePHP behavior or is there something I could do to make it work(auto populate)? I have no problem with the jQuery (already tested it in several ways), the real problem is into getting this field populated back after form submission. Any help is much appreciated.
CakePHP populates forms with the data of the form's model. So if you create a form for 'Briefing' creation, CakePHP will look at the $this->data['Briefing'] array, and populate inputs matching the fields that are present in this array. So as your hidden 'tab-active' field is present inside the 'Briefing' Form creation, CakePHP expects your 'Briefing' model to have a 'tab-active' attribute.
So my guess is that your $this->data array should be :
array(
'Briefing' => array(
'tab-active' => 'tab-4',
'title' => 'My test title'
)
)

Ajax Submit in Yii

I am using a CActive Form and, earlier i was having a normal submit button. Then I changed it to an Ajax Button like as follows,
CHtml::ajaxSubmitButton('Continue',CController::createUrl('//shop/order/create&store=true'),array('type'=>'POST'));
This goes to the url when u refresh the page. But i want it to go to the page without page refresh. Any idea how do it?
Thanks in advance.
try this, with some ajax success functionallity...
echo CHtml::ajaxSubmitButton('BottonLabel',
Yii::app()->createUrl('your/url',array('calltype' => 'system')),
array('type' => 'POST',
'cache' => true or false,
'success' => 'js:function(data) {yourJSfunction(data)}'),
array('id' => 'the_id'));
or follow this link to the class-reference for yii
ajaxSubmitButton
try this..
array('contoller_name/action','ajax'=>'somevalue'));
//In your case
echo CHtml::ajaxSubmitButton('Continue',array('contoller_name/action','ajax'=>'yw0'));
Just keep in mind that you are obviously setting auto created widget ids as params (yw0). You should assign your own ids to widgets, otherwise another widget could have the same id and that can be hard to debug..

How to Update two text fields onchange of a dropdown list

Im building a web app using the yii framework. I have a dropdownlist and im calling an action and updating a div tag using ajax array 'update'=>'#price' field. the code works fine and it updates the price div.
But i want to update two fields like that, i tried passing an array to the update field. but it didnt work.
Any Idea how I can update two div tags and show two values using one action call?
Heres My Code..
echo CHtml::beginForm();
echo CHtml::dropDownList('amount_'.$position,'', array(1=>1,2=>2,3=>3),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('shoppingCart/updateAmount'),
'update'=>'#price_'.$position, //selector to update
)));
echo CHtml::endForm();
and in my action im just echoing
echo 'LKR '.Shop::priceFormat(#$product->getPrice($cart[$position]['Variations'], $value));
It'll be great if someone could help.
It's simply jQuery selector. I believe you can use comma for few ids. Or you can use class selector. (I think class selector would be better here)
'ajax' => array(
/* ... */
'update' => '#price_1, #price_2, #price_3',
/* or */
'update' => '.price'
)

Passing an id to a method via a post form in CakePHP

I have the following scenario:
A user is viewing a post on my application at the following url: /posts/Post_tile-210
and I want them to be able to create comments against this post. This will be done by creating a relationship between the comment and post using the post id as a foreign key in the comments table.
My question is that I see three possible ways to pass the post id to add comment method:
1.) Pass the post id via the form action like: /comment/add/id:210
2.) Pass the post id via a hidden field like: <input type="hidden" name="id" value="210">
3.) Grab the post id in the controller method via the url itself
What is the best way and what are the pros and cons? I like the first the best as it seems nice and easy to setup and feels more consistent with query/named params in Cake Apps.
Since you have post_id in your Comment model and table, is easier to use hidden field:
echo $this->Form->input('post_id', array('type' => 'hidden', 'value' => $post_id));
And when you submit this form you get a complete array ready for you to save the comment with the post foreing key.
You can do it either way. You can post it as a hidden field:
echo $this->Form->input('post_id', array('type' => 'hidden', 'value' => $post_id));
or as parameter on the submission:
echo $this->Form->input('Post', array('type' => 'post', 'url' => array('controller' => 'posts', 'action' => 'add', $post_id)));
Making the post_id a hidden field will give you the advantage of already including the post_id in the request without requiring any additional code to use it with the $this->Model->save, because it will be included as $this->data['Comment']['post_id']. This means that you will only need to call $this->Comment->save($this->data); and it will save it to the table.
If you pass it on the URL as a parameter, you will then need to set it so it will save in one of two ways:
$this->data['Comment']['post_id'] = $post_id;
or
$this->Comment->post_id = $post_id;
Therefore, the best practice would be to include it as a hidden field in the form.
I would set up the controller method like:
function add(post_id){
And pass it like
URL/comment/add/variable

Categories