CakePHP: Ajax view problem (nothing in the view showing) - php

I created a function in my controller called addToPlaylist($songName). I wanted to add these song names to an array and then a session variable using an Ajax call. The first time i did this i got an error saying i do not have a template file to display in the chosen div ("add_to_playlist.ctp was missing"). I created this file and everything seemed to be working correctly. Basically I went to bed woke up and it is broken (it is possible I changed something before I went to bed). The problem now is that it does not show anything when i click the ajax link. when i click on the ajax link it seems to call the function but nothing displays in the view (except debug info) even when i delete the view (add_to_playlist.ctp) i get no errors, I just see debug info now.
Ajax Link in the View:
echo '<div class="albumName">'. $ajax->link(
'+ add song',
array( 'controller' => 'songs', 'action' => 'addToPlaylist', $song['Song']['name'] ),
array( 'update' => 'playlistInfo')
).'</div></div>';
controller function:
function addToPlaylist($songName = null){
$this->set('name', $songName);
}
Ajax view file:
<html>
<body>
<?php echo name; ?>
</body>
</htmml>

Try echoing the actual variable:
<?php echo $name; ?>

Related

Error Displayed with JToolbar DeleteList Static Function

I am building a Joomla 2.5 component and have a bit of trouble getting the Delete button to function properly. Here is a sample code from the view.html.php file:
if ($canDo->get('core.delete'))
{
JToolBarHelper::deleteList('You Really Wanna Delete that', mycomponentname.delete, 'JTOOLBAR_DELETE');
When I select an item from a dropdown list and click to delete I get the following pop-up:
You Really Wanna Delete that
The problem with this is when I click the option to verify the deletion from the pop-up I am redirected to a 500 error message and the item is not deleted.
Now when I review the Joomla documentation here:
http://docs.joomla.org/JToolBarHelper
I see that JToolBarHelper is defined in administrator/includes/toolbar.php. So I went for a visit over to review the deleteList info there. I see the following code:
public static function deleteList($msg = '', $task = 'remove', $alt = 'JTOOLBAR_DELETE')
{
$bar = JToolBar::getInstance('toolbar');
// Add a delete button.
if ($msg) {
$bar->appendButton('Confirm', $msg, 'delete', $alt, $task, true);
} else {
$bar->appendButton('Standard', 'delete', $alt, $task, true);
}
}
So I have attempted to adjust my script by changing the second parameter $task = 'remove' to read as remove rather than mycomponentname.delete as follows:
JToolBarHelper::deleteList('You Really Wanna Delete that', 'remove', 'JTOOLBAR_DELETE');
This will eliminate the 500 error, but the item is not removed. What am I missing here? My guess is that it has something to do with improperly configuring the mycomponentname.delete function.
PS- I should add that the 500 error states:
Layout default not found
There is only one problem you have. You don't need to put the component name on to the button task. You need to put controller name instead of component name.
if ($canDo->get('core.delete'))
{
JToolBarHelper::deleteList('You Really Wanna Delete that', 'controllerName.delete', 'JTOOLBAR_DELETE');
}
For example :
JToolBarHelper::deleteList('delete', 'hellos.delete','JTOOLBAR_DELETE');
Hope this helps you.

Ajax request won't trigger. [Yii framework]

I have a little problem with a Controller method AJAX call in Yii. The thing is that I'm trying to filter the data of one dropDownList based in the value of a previous selected item.
In the view file, where I figured out is the source of the problem, I have this piece of code:
<?php echo $form->labelEx($model,'Estado'); ?>
<?php echo $form->dropDownList($model,'estado',CHtml::listData(Estado::model()->findAll(),'id','nombre'),array(
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createAbsoluteUrl('buscar/select'),
'update'=>'#'.CHtml::activeId($model,'tbl_municipio_id'),
),
'class'=>'form-control'
));
?>
<?php echo $form->error($model,'Estado'); ?>
On the Controller side, I got this:
public function actionSelect(){
echo "Hello world";
$data = Municipio::model()->findAll('tbl_estado_id=:tbl_estado_id',
array(':tbl_estado_id'=>(int) $_POST['Consultorio_estado']));
$data = CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
The ajax call to the Select method isn't triggered when the dropDownList is clicked. I tracked the request using Firebug and no error nor fail message is dropped.
Anyone knows what can I do?.
Thanks in advance.
With my knowledge in Yii 1.1.13, there is no such option for ajax for form->dropDownList, just Chtml::dropDownList does.
Therefore you have option to manually custom event change of form->dropDownList or add more jQuery script to handle it by yourself, or simply switch to use Chtml::dropDownList like below example
<?php
echo CHtml::dropDownList('inst_province','',
array(1=>'A',2=>'B',3=>'C', 4=>'D'),
array(
'prompt'=>'Select City',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('city/selectAll'),
'update'=>'#city_area',
'data'=>array('city_param'=>'js:this.value'),
)));
?>
http://www.yiiframework.com/wiki/429/an-easy-solution-for-dependent-dropdownlist-using-ajax/

CakePHP: "add" action doesn't work with admin prefix

Before adding admin prefixes/routing, everything was working fine...
Currently, I have a QuestionsController.php file with the following function:
public function admin_add() {
if ($this->request->is('post') ) {
$this->Question->create();
if ($this->Question->save($this->request->data)) {
$this->Session->setFlash('Your question has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your question.');
}
} else {
$this->Session->setFlash('Not post.');
}
}
Here is the contents of /views/Questions/admin_add.php:
<h2>Add a question</h2>
<?php
echo $this->Form->create('Question');
echo $this->Form->input('nickname');
echo $this->Form->input('content');
echo $this->Form->input('option1');
echo $this->Form->input('option2');
echo $this->Form->input('option3');
echo $this->Form->end('Save question');
echo $this->Html->link('Cancel', array('controller' => 'questions', 'action' => 'index'));
Notice the setFlash("Not post.") at the bottom of the controller? Every time I click the "Save question" button I see that message? Why?
UPDATE
We've determine that the request method is get, which explains why it's not working. But now the real question is why is it get. I'm pretty sure it was post before adding the admin prefix.
<?php echo $this->Form->create('Question', array( 'type' => 'POST' ) ); ?>
Try that :)
You can also add other options to that $options array, such as action, encoding, defaults, url, etc.
UPDATE
From your comments, I think you are telling us that the GET is determined from the controller. Examine your FORM in your source code to see if type="post" is there, or if it says type="get".
If it is posting, then you are being redirected on post, similar to a PRG pattern. This is where you are losing it. What URL do you ultimately end up on after POST'ing your form?
If it's hitting the second half of the if block, the the request isn't post.
To find out WHAT it is, just add this code just before the if block:
debug(CakeRequest::method());
(assuming your debug level is 2 for development mode)
Then, once you know what kind of request is happening, check against that.

ZEND: Cannot Access Post Data

Greets.
{{Solved}}
In order to get POST data you MUST use $this->formHidden in order to hold all the data. Even though variables are inside the they don't get passed if they aren't in some sort of form item.
I am unable to access post data in ZEND.
Path of User -
START
INDEX PAGE
->Submit Page
->Pay Page
I created a controller, extended the Zend Controller, and added an action called payAction. The user can go from the Index Page to the Submit Page. After I have all their data inside variables, I used a form and a submit button to go to the "pay page". However, I cannot get any of the POST data.
public function payAction()
{
$data = $this->getRequest();
}
I have tried putting getRequest, getParam, getRawBody inside that controller function.
In the page itself I have tried.
echo 'Hello';
echo $request;
echo $data;
echo $_POST['payZip'];
echo $_POST['data'];
echo $_POST[$data];
echo $request;
echo $this->values['payZip'];
echo $payZip;
echo $this->values['shippingDone'];
echo $stuff;
Is there ANYTHING I can place in my controller or in my view in order to get my post data? I used a form method="post" and a button and it DOES allow me to get to the page. I can see Hello. But NONE of the post data is available.
Any assistance would be appreciated.
Thank you.
-- Update
$data = $this->getRequest();
$param = $this->_request->getParam('payZip');
if($this->getRequest()->isPost())
{
print_r($this->_getAllParams());
echo $param;
}
Doing that gives me -
HelloArray ( [controller] => wheel [action] => pay [module] => default [shipping] => UPS Ground [payPal] => Secure Payment System )
But I still can't print payZip... I did echo and nothing comes out.
To get parameters from Zend Framework you need to do this in the Action Controller:
$data = $this->_request->getParams();
You can also get individual params like this
$param = $this->_request->getParam('payZip');
What it appears your doing wrong is you're only getting the "request object". You need to then call that objects method to get the request data.
Here's some simple code I often use when testing parameters:
public function indexAction()
{
#::DEBUG::
echo '<pre>'; print_r($this->_request->getParams()); echo '</pre>';
#::DEBUG::
}
This will show all your parameters. What you will notice is that you will also get the names of the Module, Controller and Action with your params.
EDIT
Ps. If you're trying to use the parameter in the view script you need to do this:
echo $this->data['payZip'];
echo $this->param;
In your Action Controller, you save your data to the "view" object by doing this:
$this->view->myVariable = 'Hello';
But when you're in a view script, you are IN the view script, so $this represents $this->view from the action controller.
So, you access the variable like this:
echo $this->myVariable;
Wrapping everything into a bigger code chunk for understanding:
Your Controller
public function indexAction()
{
// get all parameters and pass them to the view
$this->view->params = $this->_request->getParams();
// get an individual parameter and pass it to the view
$this->view->payZip = $this->_request->getParam('payZip');
}
Your View Script
<!-- Dump all parameters -->
<pre><?php print_r($this->params); ?></pre>
<!-- Print payZip -->
<p>My PayZip is: <?php echo $this->payZip; ?></p>
<!-- Print payZip from full parameter array -->
<p>My PayZip (array) is: <?php echo $this->data['payZip']; ?></p>
I hope that helps!

jQuery("body").undelegate is not a function error

I have a form that works perfectly. Now, when i add the following code, it throws the jQuery("body").undelegate is not a function error.
<?php
echo CHtml::dropDownList(
'country_id',
'',
array('0'=>'Choice One',
'1'=>'USA',
'2'=>'France',
'3'=>'Japan',),
array(
'ajax'=>array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('users/aTest'),
'success'=>'function(data){alert(data)}',
)));
?>
In UsersController, I have the following action (and it is also included in the accessRules)
public function actionATest()
{
echo "this is a test";
}
When i expand the error, a specific line of code is highlighted (the second one)
jQuery(function($) {
$(".errorDisplay").animate({opacity: 1.0}, 3000).fadeOut("slow");
jQuery('body').undelegate('#country_id','change').delegate('#country_id','change',function(){jQuery.ajax({'type':'POST','url':'/index.php/users/unaPrueba','success':function(data){alert(data)},'cache':false,'data':jQuery(this).parents("form").serialize()});return false;});
Any ideas??? Anybody had this problem? Couldn't find it anywhere!
Check that your jQuery includes aren't conflicting. If you're adding jQuery with your own tag, Yii might still be automatically adding another jQuery file you weren't expecting, as part of its built-in assets (see this question). You can disable Yii's jQuery:
'components'=>array(
'clientScript'=>array(
'scriptMap'=>array(
'jquery.js'=>false,
),
),
)

Categories