Dynamic Dropdown in yii - php

I followed the instructions given in the wiki in creating dynamic dropdowns in yii but still the second dropdown doesnt generate,
here is my view file:
echo CHtml::dropDownList('AccountTypeID','', array(1=>'Admin',2=>'Manager',3=>'Business',4=>'Finance',5=>'Customer Support'),array('ajax' => array('type'=>'POST',
'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.
'update'=>'#city_id', //selector to update
)));
echo CHtml::dropDownList('city_id','', array());
and here is my controller:
public function actionDynamiccities(){
$data= RefAccountgroup::model()->findAll('parent_id=:parent_id',
array(':parent_id'=>(int) $_POST['AccountTypeID']));
$data=CHtml::listData($data,'AccountGroupID','AccountGroupName');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}}

Please pay attention on the 'ajax' declaration
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.
'update'=>'#city_id', //remember to change it according to this rules #modelname_columnname
'data'=>array('AccountTypeID'=>'js:this.value'), //I think you missed this line
)));
If you have firebug installed on your firefox browser, try to look up at the console tab, and check whether your dropdownlist change successfully sent out parameter AccountTypeID and you can see the responses as well.

First things first, your question would have been improved if you actually included the link to what tutorial you tried to follow.
Second, when answering the question in the comment, "It didn't sent a data" doesn't really clarify anything. At this point, I'm not sure if you even looked in Firebug or any web developer tool to inspect what was being posted and what was being received. My guess is that if you would have looked you would have seen a POST request being made, with no data being sent, and no data being returned.
Your AJAX call doesn't have a data parameter, so $_POST['AccountTypeID'] will not be defined, meaning that $data in your controller will be empty as well.
Update your AJAX to POST the AccountTypeID, e.g.:
echo CHtml::dropDownList('AccountTypeID',
'',
array(1=>'Admin',2=>'Manager',3=>'Business',4=>'Finance',5=>'Customer Support'),
array('ajax' => array('type'=>'POST',
'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.
'update'=>'#city_id', //selector to update
'data'=> '$("#AccountTypeID").serialize()',
)));
echo CHtml::dropDownList('city_id','', array());
You may need to adjust the data parameter to properly parse the correct input to get the AccountTypeID - I'm just guessing in my sample code above.
Then, while testing, make sure to use Firebug or similar to inspect what's getting POST'ed and make sure that you have AccountTypeID being passed.

Is simple. You need to declare a public variable on your model with the same name as the dropdownlist. Ex.
in your model
public mydropdown;
in your view
echo $form->dropDownList($model, 'mydropdown',array('1'=>'hello','2'=>'bye'));

Not sure if this helps, but...
echo CHtml::dropDownList('city_id','', array( 'id' => 'city_id' ));

Related

Laminas toRoute as POST?

I work with Laminas and in a ControllerAction function I want to redirect to another url as a post by using $this->redirect()->toRoute('newSite', $noURLParams, $options);
When I use the "query"-Option ($options = [$query => ['postPara1' => 'blabla', 'postPara2' => 'blubblub']]), I will get the "post"-Parameter as GET-Parameter in the URL:
hhttp://localhost/new-site?postPara1=blabla&postPara2=blubblub
So everyone could see the content of the parameters.
Is there a way to get the toRoute() to make a "POST" out of it?
Or do I have to use another function for this?
Thank you for your interest and help. After you said that it doesn't work, I changed the sequence of what, when should be called and now I can do it without an additional redirect.

In Slim, how do I pass a GET variable to render

I've only been working with Slim for a short while. I'm still on version 2. So far everything has been going just fine, but I've hit a little snag. I have a page that displays content based on a GET variable at the end of the URL. The url looks like the following...
http://localhost/trailcache.com/checklist/21
The first line of the get route looks like this...
$app->get('/checklist/:Id', function($Id) use($app) {
It ends like this...
})->name('checklist');
That Id parameter controls what info I'm pulling into the page and everything has been going just fine, but now I've added a contact form and with it, some validation. I'm writing to the DB and rendering the new content okay. The problem arises when I try to send errors back to the page. Currently it looks like this...
$app->render('user/checklist.php', [
'Id' => $Id,
'errors' => $v->errors(),
'request' => $request
])->name('checklist');
This doesn't work. The page is blank. The url it returns is...
http://localhost/trailcache.com/checklist
The documentation for render shows...
$app->get('/books/:id', function ($id) use ($app) {
$app->render('myTemplate.php', array('id' => $id));
});
Wouldn't that work in the post route the same way? It has on all my other pages.
How can it get pass those errors along with the correct GET variable so that the correct content displays?
This works for me just fine.
return $this->renderer->render('myTemplate.php', array('id' => $id));
Hope it works for you.
After a few days of struggling with this, I found a solution. I first discovered map() and changed the first line to...
$app->map('/checklist/:Id', function($Id) use($app) {
and the end to...
})->via('GET', 'POST')->name('checklist');
so now the route renders the same whether it's coming from post or get. I then pointed the form to this named route...
<form action="{{ urlFor('checklist', {Id: Id}) }}" class="form" method="post">
and moved all the post logic inside this route. Since there is no need to validate if the request wasn't a post I used...
if($app->request->isPost()) {
and put the validation code inside. Refreshing the page after making a form submission would resubmit the form so if validation passed I added...
return $app->response->redirect($app->urlFor('checklist', array(
'Id' => $Id
)));
All I had to do was pass the errors to the template and it seems to be working perfectly.

(#100) At least one reference object must be specified - Simple POST of OG action

I am using the PHP SDK to try and post a simple action using the Graph API.
I have set up an object called 'Competition' and an action called 'Enter'. As such, a user enters a competition.
My code is like so:
try {
$statusUpdate = $this->data['facebook']->api('/me/lovepoppycompetition:enter', 'post', array(
'competition' => 'https://apps.facebook.com/lovepoppycompetition/'
));
echo '<pre>SUCCESS: '.print_r($statusUpdate, true).'</pre>';
} catch(FacebookApiException $e) {
echo '<pre>FAILED: '.print_r($e, true).'</pre>';
}
I am getting an error returned (FacebookApiException):
(#100) At least one reference object must be specified
I'm not really sure why, especially as I am using this code (with different objects/actions) on another app with no issues just fine.
It might be worth noting that I haven't submitted the action, although I don't think I have to for it to work for me?
Thanks in advance.
I think the problem lies with you redirecting the action back to your facebook application. You should create a competition page on your website, and use that to redirect users back to your website.
Make sure you use the correct og tags on the redirect page.
This was caused by a FB bug, which is now resolved

Default value not overridden upon form submission

I've got a hidden field inside a form, which I create using Drupal's form API like this:
$form['position'] = array(
'#type' => 'hidden',
'#default_value' => '57.149953,-2.104053',
'#attributes' => array(
'id' => 'geoposition',
)
);
When I load the page in which the form is rendered, I have a little bit of JavaScript that edits the hidden field like so:
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getPosition, noPosition, 10);
}
function getPosition(position) {
var pos = document.getElementsByName("position");
pos.value = position.coords.latitude + "," + position.coords.longitude;
alert(pos.value);
}
Now, the last statement in this snippet actually outputs the correct value of the user's current position. However, when I submit the form, the value passed onto the server for manipulation is the default value, and not the updated value. The value to use after the form has been submitted is picked up like this:
$map_field .= 'src="http://maps.google.co.uk/maps?q='.$form_state['values']['position'].'&output=embed"></iframe>';
Any ideas?
Thanks,
This is what I wud do:
1) Disable to js for now.
2) Use xdebug to see the value before and after the form has been submitted. This should help to see if something is going wrong.
Cheers,
vishal
Rather than using #default_value, consider using #value - because that is the value that gets posted.
Eventually solved it by using the getElementById() method instead of getElementsByName() method. For some reason the name wasn't working. Thanks all for your help.

Do query strings need to be manually sent to POST requests

This is a CakePHP / General PHP question.
In my application I use a query string like /login?continue=/admin/posts
This query string is used to redirect users to the URL in the query, but it doesn't work so it seems as though the app can't see the string...
This has got me wondering as basically when you arrive at the page with the string it's a GET request where as when you login, it becomes a POST or XML request (if using AJAX). Do I need to add the query string manually to the form for the POST to see it?
Either in the form action or a hidden input? Or am I barking up the wrong tree?
I'm currently grabbing the query like so:
if(isset($this->params['url']['continue']))
{
$pathtoredirect = $this->params['url']['continue'];
}
else
{
$pathtoredirect = $this->Auth->redirect();
}
But that's within the POST request so perhaps the query is lost... and adding it to a hidden input would not solve the problem with the current code so I would either change the code to look at the hidden field or pass the query with the action on the form?
e.g. <form action="/login?continue=/admin/posts" method="post">
Am I correct in thinking this? And would anyone be able to offer solutions or pros and cons of the two methods I mention?
In short I'm asking how to add the query string to my form action value
It currently looks like:
php echo $this->Form->create('User',
array(
'id' => 'loginform',
'type' => 'post',
'url' => array
(
'admin'=>false,
'controller' => 'users',
'action' => 'login'
)
)
);
So how would I add the query string to the form?
Thanks
When receiving a POST request you can receive both POST and GET variables through the superglobals $_POST and $_GET.
You can either send your paramater in $_GET by including it in the form's action attribute or send it in $_POST by creating an <input type="hidden"> tag within the form
A slash (/) is a reserved character. Encode it with %2F.
http://blooberry.com/indexdot/html/topics/urlencoding.htm#whatwhy
The solution was to do this:
<?php echo $this->Form->create('User',
array('id' => 'loginform', 'type' => 'post',
'url' => array('admin'=>false,'controller'=>'users','action'=>'login','url'=>$this->params['url']['continue']))); ?>
as I have a route already setup to handle the additional URL parameter:
Router::connect('/auth/login', array('controller'=>'users','action'=>'login'));
Router::connect('/auth/login?continue=:url',
array('controller'=>'users','action'=>'login'),
array(
'url' => '[A-Za-z0-9/\._-]+',
'pass' => array('url')
));
If anyone sees any issues with the way I do this though, please feel free to comment.

Categories