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