Rob,

That's the point of ending the 'name' attribute with the empty array
brackets '[ ]' as described above.

Granted, this will return your data in an array structure that will
require a little re-arrangement as mentioned.

Regards
Don

On May 22, 6:16 pm, Rob <[email protected]> wrote:
> Hey, I havent abandoned this issue.
>
> Armed with what I got from reading the replies here I went on some
> google searches and came up with a near-solution that is almost there.
>
> My first main problem was that this is my first foray into AJAX, so my
> lack of understanding was a hurdle there.  The other problem was that
> I have been away from cakePHP for a while so some obvious things ended
> up getting me snagged for a bit.  Anyway here are the steps I took,
> and my new problem.
>
> I created the view for the form that, using a Js helper request, gets
> appended to the original form when the user clicks a button:
>
> <fieldset>
> <?php
> $this->Form->create();
> echo $this->Form->input('Element.1.element_title_id', array('label' =>
> 'Element Type', 'options' => $element_titles));
> echo $this->Form->input('Element.1.title', array('type' => 'text'));
> echo $this->Form->input('Element.1.description', array('type' =>
> 'textarea', 'label' => 'Description'));
> echo $this->Form->input('Element.1.Image.0.path', array('type' =>
> 'file'));
> ?>
> </fieldset>
>
> Ajax calls the add_element action of my projects_controller which just
> has a simple $this->render('add_element', 'ajax') statement in it.
>
> This all works beautifully, and believe it or not took me a while to
> figure out (i tried some horrible horrible things before I got to
> where I am)
>
> Now this is what the $data looks like when I submit the form:
>
> Array
> (
>     [Project] => Array
>         (
>             [title] =>
>             [description] =>
>         )
>
>     [Element] => Array
>         (
>             [0] => Array
>                 (
>                     [element_title_id] => 3
>                     [title] =>
>                     [description] =>
>                     [Image] => Array
>                         (
>                             [0] => Array
>                                 (
>                                     [path] =>
>                                 )
>
>                         )
>
>                 )
>
>             [1] => Array
>                 (
>                     [element_title_id] => 3
>                     [title] =>
>                     [description] =>
>                     [Image] => Array
>                         (
>                             [0] => Array
>                                 (
>                                     [path] =>
>                                 )
>
>                         )
>
>                 )
>
>         )
>
> )
>
> My problem is that no matter how many times a new element is added via
> my add_element action. only 2 elements are sent back to the controller
> to be saved.  I know why this is happening; Element.
> 1.element_title_id.  I'm not incrementing the key there, its always
> 1.  For example the second time the button is clikced, that should be
> Element.2.element_title_id, but I have no idea how to do that
>
> So what it comes down to is that I have no idea how to increment this
> key each time the button is clicked on the form.
>
> Any help is appreciated.
> Don't hesitate to ask me to clarify...I tend to get sloppy and
> rambling when describing something like this
>
> On May 16, 11:53 am, dreamingmind <[email protected]> wrote:
>
>
>
>
>
>
>
> > Rob,
>
> > My code diverged from crisuwork's in a couple of areas. One difference
> > (which I think you will share); I had to add several fields rather
> > than one. This is a small detail but it adds a fair amount of code
> > though the general pattern he describes is the same I followed. It
> > sums up the process beautifully.
>
> > Another difference: when I add a group of fields I have to do some db
> > checks to see if data exists for the image name I'm bringing in. That
> > means I may need to populate some fields with existing data so I don't
> > rewrite or over-write things in ignorance. And I have select lists to
> > populate, etc.
>
> > These differences in complexity lead to the other difference: I set
> > myself the goal of following web standards and cake conventions as
> > closely as possible. This helped manage the complexity. Rather than
> > introducing the new form fields with a javascript function, I used an
> > Element and had my javascript return that via Ajax.
>
> > So my basic pattern is:
> > User clicks link calling javascript/ajax
> > Js finds form content existing? clears it out (this allows me to
> > toggle sections open/closed)
> > Js finds no content then it uses my Cake action to look up data and
> > return an Element containing a populated fieldset
> > Sends it all back to the page.
>
> > One important detail that crisuwork had in his code which you'll need
> > to note; the name attribute for his field ends with [ ]. This will let
> > your multiple occurrences of the same field return as an array. If the
> > fields have the same name attribute and don't end with the array
> > brackets, they'll overwrite each other and you'll get back the value
> > of the last one.
>
> > This caused me some problems in the Element because the Form Helper in
> > its simplest usage is incompatible with our needs in this case. The
> > Cake Development team was farsighted though!
>
> > Problem:
> > echo $this->Form->input('ImageId'); // cake code in the Element
>
> > Returns:
> > <div class="input text">
> >   <label for="ImageId">Image Id</label>
> >   <input
> >       type="text"
> >       id="ImageId"
> >       name="data[ImageId]"
> >   >
> > </div>
>
> > Both the normally returned 'id' and 'name' attributes will trip you up
> > in the kind of ajax function we're discussing (id's need to be unique
> > or things can break).
>
> > Solution, this more detailed cake code in the Element:
> > echo $this->Form->input('ImageId', array(
> >     'name' => 'data[Image][id][]',
> >     'id' => null,
> >     'class' => 'imgid',
> >     'type' => $type
> > ));
>
> > Returns this simpler html:
> > <div class="input text">
> >    <label for="ImageId">Image Id</label>
> >    <input
> >      type="text"
> >      class="imgid"
> >      name="data[Image][id][]"
> >    >
> > </div>
>
> > As you can see, both our problems are solved! You can add a 'value'
> > attribute also if you want to force field content. But, since this is
> > standard Form Helper usage, if the standard data array exists (from a
> > query in the controller) fields will get their appropriate content.
>
> > By the way, the 'type' attribute is just a debugging thing so I can
> > make certain fields hidden or visible as I work things out.
>
> > crisuworks describes the process you'll need to follow when processing
> > the returned data.
>
> > Regards,
> > Don
>
> > On May 15, 11:42 pm, crisuwork <[email protected]> wrote:
>
> > > I just made such a feature 2 weeks ago using a simple javascript
> > > function to generate more input fields with file as type. I attached
> > > that function to a button "Add files" and that is.
> > > You need than in controller to write a loop to parse all the array
> > > from $_FILES and for each file do call a "save()" function.
>
> > > Here is an example jQuery function ( this function has to be in your
> > > ctp file ):
> > > function addmorefiels(){
> > >     $('#yourDiv').append('<input type="file" name="myfilesforupload[]"
> > > value="">');
>
> > > }
>
> > > The you need in your form .ctp file some div where you push/attach new
> > > dynamically input fields:
> > > <form....>
> > > ...
> > > ...
> > > <div id="yourDiv"></div>
> > > <a href='javascript:void(0)' onclick='addmorefiels()'></a>
> > > </form>
>
> > > Than, in controller in save() function you check the array from
> > > "myfilesforupload":
>
> > > if( is_array($_FILES["myfilesforupload"]) &&
> > > count($_FILES["myfilesforupload"])>0   ){
> > >    foreach($_FILES["myfilesforupload"] as $distinctFile){
> > >        ... here you have to call your upload class and to save the
> > > files
> > >    }
>
> > > }
>
> > > If you want to do this without page refresh than you have to use $ajax
> > > from jQurey :)
>
> > > On 15 Mai, 22:22, Rob <[email protected]> wrote:
>
> > > > Let me start off by explaining what I want to do and where I am right
> > > > now.
>
> > > > I have a CakePHP site where I can submit projects, and each project
> > > > has many different elements attached to it.  I'm created the add
> > > > project form to be able to add elements as well.  I have successfully
> > > > created a form that will submit both projects and elements.  This form
> > > > has 2 parts to it the Project form which there is only one and the
> > > > Element form which I want there to be many of.   So for one project
> > > > submission there will be more than one element submission.
>
> > > > What I want to do is add a button that, when clicked, adds another
> > > > Element form to the page until there are enough.  My original idea was
> > > > to do this with AJAX and jQuery, since the form creation is handled by
> > > > cake.  Problem is, I don't really know where to get started.
>
> > > > Any push in the right direction would be helpful
> > > > Thanks

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to