views:

16

answers:

1

Hello,

I am learning cakephp, and I was just doing a simple Todo list app from a book. The app basically takes in a task from an input field, then you click 'Add Task' and the task should be added to database and then the user gets redirected to Tasks View page where all the tasks can be seen including the one they just added. In the add task view (add.ctp) there is an "Add Task" button. The button shows up in the browser, but it does nothing when clicked on, it doesn't even seem to be linked to anything?? Here is my code:

//Model: task.php
<?php
    class Task extends AppModel
    {
    var $name = 'Task';
    }
?>

//Controller: tasks_controller.php
<?php
    class TasksController extends AppController
    {
        var $name = 'Tasks';
        var $helpers = array('Html', 'Form');
    function index()
    {
        $this->set('tasks', $this->Task->find('all'));
    }

        function add()
        {
            if (!empty($this->data)) {
                $this->Task->create();
                if ($this->Task->save($this->data)) {
                    $this->Session->setFlash('The Task has been saved');
                    $this->redirect(array('action'=>'index'), null, true);
                } else {
                    $this->Session->setFlash('Task not saved. Try again.');
                }
            }
        }
    }
?>

//View: views/tasks/index.ctp
<h2>Tasks</h2>
<?php if(empty($tasks)): ?>
    <p>There are no tasks in this list</p>
<?php else: ?>
    <table>
        <tr>
            <th>Title</th>
            <th>Status</th>
            <th>Created</th>
            <th>Modified</th>
            <th>Actions</th>
         <tr>
         <?php foreach($task as $task): ?>
            <tr>
                <td>
                    <?php echo $task['Task']['title'] ?>
                </td>
                <td>
                    <?php
                        if($task['Task']['done']) echo "Done";
                        else echo "Pending"
                    ?>
                </td>
                <td>
                    <?php echo $task['Task']['created'] ?>
                </td>
                <td>
                    <?php echo $task['Task']['modified'] ?>
                </td>
                <td>
                </td>
            </tr>
          <?php endforeach; ?>
    </table>
<?php endif; ?>

//View: views/tasks/add.ctp
<?php $form->create('Task'); ?>
<fieldset>
    <legend>Add New Task</legend>
    <?php
        echo $form->input('title');
        echo $form->input('done');
    ?>
</fieldset>
<?php echo $form->end('Add Task'); ?>

//My Database setup
CREATE TABLE `tasks` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `done` tinyint(1) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Hope thats clear enough. This is a very simple app, so I'm sure the fix is easy. In the add task view (add.ctp) there is an "Add Task" button. The button shows up in the browser, but it does nothing when clicked on, it doesn't even seem to be linked to anything??

Thanks in advance, I appreciate any help.

+3  A: 

At first glance it seems that you forgot to echo the opening form tag in views/tasks/add.ctp Try replacing

<?php $form->create('Task'); ?>

with

<?php echo $form->create('Task'); ?>
Mike