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.
Related
Im working on a piece of code to alter permissions for users of an app. Currently, each permission is added one at a time, Im revising it to be added in a faster way, being able to select multiple permissions to add at once.
The checkboxes aren't going to be the same number, since they are only the permissions the user doesn't yet have. It needs to pass an id with the permissions.
The problem is, my checkbox form doesn't appear to be sending any data to the controller. Upon submitting, it just loads a blank page with the url of the controller.
Heres the view code, that generates the checkbox form
<?php
if (!empty($lstAvailablePermissions)) {
$c=0;
echo $form->create('Administrator', array('action'=>'addPermission'));
echo $form->input('id');
foreach($lstAvailablePermissions as $key){
echo "<br>";
echo $form->input(
'permission',
array(
'id'=>$key,
'label'=>$key,
'type'=>'checkbox',
'multiple'=>'checkbox',
'value' => $key,
'name' =>'data[Administrator][permission]['.$c.']'
));
$c=$c+1;
}
echo $form->button(__('Add', true), array('type'=>'submit', 'class' => 'button', 'style'=>'padding: 2px; font-size: 12px;'));
echo $form->end();
}
?>
and the method in the controller
function AddPermission() {
if (empty($this->data)) { $this->RedirectWithFlash(__("Only POST Requests", true), "/administrators"); }
ErrorLogWarning("This is the form data sent to the controller", $this->data);
$length=count($this->data['Administrator']['permission']);
for ($i = 0; $i < $length; $i++){
$this->Acl->allow(
array('model' => 'Administrator', 'foreign_key' => $this->data['Administrator']['id']),
$this->data['Administrator']['permission'][$i]
);
}
$this->RedirectWithSuccessFlash(__("Permission added", true), array('action'=>'edit', $this->data['Administrator']['id']));
}
can anyone help me figure out whats going on? I assume ill need to loop through the data once its in the controller, but it doesn't even send the error message as of right now.
EDIT: problem appears to be fixed by adding the name property at the bottom of the form echo, which send it as an array which can be looped through in the controller.
I think the problem might be that you specify 'action'=>'addPermission' when you create the form, but your controller action is named AddPermission (capital A). Try changing to 'action'=>'AddPermission'.
Also, I think you might only get the last permission input because you're adding the permission inputs in a loop. Try indexing the inputs like this:
foreach($lstAvailablePermissions as $i => $key) {
echo "<br>";
echo $form->input(
"Administrator.{$i).permission",
I edit a row on the Post table or add a new row to it from edit() function in PostsController. The function looks like this:
public function edit($id = null) {
// Has any form data been POSTed?
if ($this->request->is('post')) { //Replaced 'post' by 'get' in this line
// If the form data can be validated and saved...
if ($this->Post->save($this->request->data)) {
// Set a session flash message and redirect.
$this->Session->setFlash('Updated the Post!');
return $this->redirect('/posts');
}
}
// If no form data, find the post to be edited
// and hand it to the view.
$this->set('post', $this->Post->findById($id));
}
I simply replaced 'post' by 'get' to see what would happen and it went on creating new rows without even taking me to the form. I still get the flash message 'Updated the Post!', but without taking any form data.
If the code in edit.ctp is required, here it is:
<?php
echo $this->Form->Create('Post');
echo $this->Form->input('id', array('type' => 'hidden','default'=>$post['Post' ['id']));
echo $this->Form->input('title',array('default'=>$post['Post']['title']));
echo $this->Form->input('body',array('default'=>$post['Post']['body']));
echo $this->Form->end('Update');
?>
Any thoughts on why this might be happening?
Edit: Added CakePHP Version
I am using CakePHP 2.4.5
What you are doing makes no sense.
Why would you want to switch the "post" by "get" here?
Of course it will then generate new rows, as you effectively trigger a save on each page load (GET).
Don't do that.
The code you had there was just fine - IF you also took PUT into consideration.
For edit forms, it is not a post, but:
if ($this->request->is('put')) {}
PS: If you want to make sure it always works for both add/edit, use
if ($this->request->is(array('post', 'put')) {}
But NEVER replace it with "get".
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.
I am starting to learn PHP framework, particulary cakephp. I know how to insert into database, but I am curious about displaying the entries on the same page. I don't want to reload or redirect the page just to display the results.
I have only three four fields in the database. id, name, email, and the date. I have used the same validation in the model page.
Controllers:
<?php
function index(){
if($this->request->is('Post')){
$this->Save->save(data);
}
//display the entries from the database
$this->set('saves', $this->Save->find('all'));
}
?>
Index.ctp:
<?php
echo $this->Form->create('Save');
echo $this->Form->input('name', array('class'=>'name', 'required' => true));
echo $this->Form->input('email', array('class'=>'email', 'required' => true));
echo $this->Form->input('message', array( 'required' => true));
echo $this->Form->submit('submit', array('formnovalidate' => true));
//i want to display the entries here
foreach($saves as $row){
echo $row['Save']['name'];
echo $row['Save']['comment'];
}
?>
The problem is that , it affects the textarea. It reduces the size to half. Need help. thanks a lot. I am new to cakephp and I have been googling about it, but have not find the any results. Thanks
From what I understand it seems that your problem is more CSS issue than CakePHP issue. I don't think printing the data will cut the field into half.
Try put after echo $this->Form->submit('submit', array('formnovalidate' => true)); because the submit method does not include afterwards.
You can this $this->Form->end if you don't want to add the form closing tag manually.
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; ?>