Reputation: 2167
I'm new to cakephp and trying to simply display form data once it is posted. I would like to type something on "add.ctp" which then redirects to "index.ctp" where the information I just typed should be displayed.
The reason why I'm doing this is because I like to echo my variables and forms at various places throughout my program for debugging purposes. I tend to work a lot with data that needs to be converted or manipulated so I like to check and make sure each part is doing its job correctly. I'm new to cakephp so I'm just trying to figure out how I can do this.
Here is the code for add.ctp where the information is entered.
View\Mysorts\add.ctp
<h1>Add Numbers</h1>
<?php
echo $this->Form->create('Mysort');
echo $this->Form->input('original');
echo $this->Form->end('Add Numbers');
?>
Here is my function in the controller
Controller\MysortsController.php
<?php
class MysortsController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
$this ->set('mysorts', $this->Mysort->find('all'));
}
public function add() {
if($this->request->is('post')) {
Configure::read();
pr($this->data); //attempting to print posted information
$this->redirect(array('action' => 'index'));
}
}
function isempty(){
$mysorts = $this->Mysort->find('all');
$this->set('mysorts', $mysorts);
}
}
?>
And finally, here is my index file where I would like to display the posted information.
View\Mysorts\index.ctp
<h1>Sorted Entries</h1>
<?php
echo $this->Html->link("Add List", array('controller'=>'mysorts', 'action' => 'add'));
if (!empty($mysorts)) {
?>
<table>
<tr>
<th>ID</th>
<th>Original</th>
<th>Sorted</th>
</tr>
<?php foreach ($mysorts as $mysort): ?>
<tr>
<td><?php echo $mysort['Mysort']['id']; ?></td>
<td>
<?php echo $mysort['Mysort']['original']; ?>
</td>
<td> <?php echo $mysort['Mysort']['sorted']; ?>
</td>
</tr>
<?php endforeach;
} else {
echo '<p>No results found!</p>';
}
?>
</table>
Upvotes: 0
Views: 13241
Reputation: 2232
If the code you have posted is the exact you are using that could not work at all.
You do not save the data you receive while being in the "add" method. This is done by $this->ModelName->save($data) where ModelName is the Model to use (in your case it should be MySort and $data is the posted data.
You are using Cakephp2.x? I assume so cause you are using $this->request->is('post') which was not there in 1.3, i think. The problem about that is, that the posted data is not stored in $this->data anymore. It is in $this->request->data.
Do not use pr(). It is too "dangerous" to forget something like that in the code. Use debug() instead. The output will be disabled as soon as you see the DEBUG constant in Config/core.php in your application root to 0.
Calling the redirect() method in a controller generates a real 301 redirect. Which means the old output is dumped and lost. That and point 1 makes clear why you can not see anything. Nothing is saved and before you see the output of pr() your browser gets redirected. If you want to debug something use an exit; afterwards to make sure you wont miss the output. Sometimes you do not need it, but if you can not find your output, use it ;)
Hope this helps you.
Greetings
func0der
Upvotes: 2
Reputation: 2005
I don't understand what your point is in trying to print something and then redirecting immediately? You won't see it when it redirects.
Anyhow, since your Form probably does not consist of a representation of an actual model, you might want to inspect your $this->params['form']
variable as opposed to the normal $this->data
that you would use in a FormHelper.
Also, do you realize you are missing a closing } in your Controller\MysortsController.php? It closes the add() function but not the class...
Upvotes: 0
Reputation: 95
Maybe what you need is something like this. For your add.ctp you define the action you want your post.
<h1>Add Numbers</h1>
<?php
echo $this->Form->create(array('action' => 'view'));
echo $this->Form->input('original');
echo $this->Form->end('Add Numbers');
?>
For your Controller You'll need to set the variable you want in your view
public function add() {
}
public function index(){
if($this->request->is('post')) {
$this->set('mysorts', $this->request->data);
}
}
And I'm not sure if what I see in your index.ctp makes sense.
Upvotes: 0