开发者

Add Database Row Button Cakephp

The Controller:

function add(){
    if (!empty($this->data)) {
        $qnote = $this->Qnote->save($开发者_如何学Gothis->data);
        if (!empty($qnote)) {
            $this->data['Step']['qnote_id'] = $this->Qnote->id;
            $this->Qnote->Step->save($this->data);
        }
        $this->Session->setFlash('Your note has been saved.');
        $this->redirect(array('action' => 'index'));
    }
}

The Form.

<?php
$userID = Authsome::get('id');
echo $form->create('Qnote', array('action'=>'add'));
echo $form->input('Qnote.id', array('type' => 'hidden'));
echo $form->input('Qnote.user_id', array('value' => $userID, 'type' => 'hidden'));
echo $form->input('Qnote.subject');
echo $form->input('Qnote.body', array('rows' => '3'));
echo $form->input('Step.id', array('type' => 'hidden'));
echo $form->input('Step.user_id', array('value' => $userID, 'type' => 'hidden'));
echo $form->input('Step.body', array('rows' => '3'));
echo $form->end('Save Notes');
?>

This Form Adds Data in 2 Models. Model 1 = Qnote; Model 2 = Step; I am able to add Data to the Models.

I was wondering I could add a button to the form The Button would allow users to add multiple Step.data to the Step model. Some like a +1 Button.

Basically I want to add multiple steps Per Qnote.

Could someone point me in the right direction how i can achieve this.


This is something I would do with jQuery. Basically all you need to do is using jQuery to dynamically add more inputs in the CakePHPs conventions: Step.0.user_id for example. What you need to do now on a +1: you need to count the zero up, so you will get Step.1.user_id and so on.

First option: Use a jQuery-Script for doing this

var count = 1;
$('#add_step').click(function() {
    var new_form = $('.Step').eq(0).clone();

    $('input, textarea, select, radio', new_form).filter('[name^="data"]').each(function() {
        var name = $(this).attr('name');
        var new_name = name.replace(/\[\d*\]/, '['+count+']');
        $(this).attr('name', new_name).attr('value', '');
    });
    $('#YourForm').after(new_form);
    count+;
    return false;
});

In this case you're cloning a div with the class step which holds your inputs for the model Step. You then replace the name-attribute to replace the zeros through the new value of the variable count. count++ enables you to add as many steps as you want.

This is an jQuery only solution and may require additional work for your environment.

Second option: Use AJAX with an element

You could also write a function in your StepsController which renders an element which holds your form and takes care of the counter.

Third option: Use a URL-parameter to decide how many options you want

If you have an URL like /qnote/add/3 you could use the 3 as a parameter in a for-loop to iterate through these form-inputs. You need to take care, that eventually already typed in values are sent with the form when adding another Step so that these don't get lost.

Hope this helps to get on the right way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜