Thursday, September 29, 2011

Re: Can't get my form in a modal box working in cakephp

Sarah,

This is not a CakePHP issue, this is related to a jQuery plugin that I
don't think there is a CakePHP helper for (may find one somewhere but
never heard of the plugin myself).

However, let's try and get you on the right path. It's one thing to
open a modal that contains a form, which it seems you're managing to
do, but you then need to ensure that the form within the modal is
being submitted as an AJAX request that updates the modal rather than
loading a new page.

I am not familiar with NyroModal so can't give specific help on that
jQuery Plugin. I've had a quick look at the plugin's documentation
and I found the demos very hard to follow:
http://nyromodal.nyrodev.com/

However, I think you simply need to add class='nyroModal' to your form
tag and it will submit the form as an ajax request, but you need to
make sure Cake is then able to handle the AJAX request properly and
send it back without the default layout and debug messages etc. I
reccomend using the RequestHandler component to check if a request to
the controller/action isAjax and then set the layout to 'ajax' which
will probably just contain <?php echo $content_for_layout; ?>. I also
strongly reccomend using FireBug when working with AJAX so you can see
if your requests are being fired, what they sent and what they
returned.

Why did you chose NyroModal over jQuery UI's excellent native 'dialog'
which creates modals that are themeable? I use these all the time and
find the documentation to be spot on.
http://jqueryui.com/demos/dialog/#modal-form

HTH, Paul

On Sep 28, 6:01 pm, "crazysarahtemple...@aol.com"
<crazysarahtemple...@aol.com> wrote:
> Hi Mike, I really really appreciate your help with this but I've been
> fiddling about and trying to understand what you've told me but I'm
> still a little bit unsure. I'm really new to JS and not too sure what
> bits go where.
>
> Heres what I have in my JS file for this page:
> $(function() {
>   $('.nyroModal').nyroModal();
>
> });
>
> As far as I know that is meant to mean that when someone clicks a link
> with the class="nyroModal" the modal pop up should appear (which it
> does:)
>
> Here's what I have to open the modal:
> <a href="#test" class="nyroModal"><?php echo $this->Html->image('uploadimage.gif');?></a>
>
>         <div id="test" style="display: none; width: 600px;">  <?php
>                 echo $form->create('Image', array('action'=>'upload', 'type' =>
> 'file'));
>                 echo $form->input('fileName', array('type' => 'file'));
>                 echo $form->end('Upload');?>
>         </div>
>
> What bits do you mean I should be changing?
>
> thanks ;)
>
> On Sep 28, 3:41 pm, mike karthauser <mi...@brightstorm.co.uk> wrote:
>
>
>
>
>
>
>
> > Hi Sarah
>
> > On 28 Sep 2011, at 15:05, crazysarahtemple...@aol.com wrote:
>
> > > I'm learning all about cakephp but having an issue which I can't
> > > figure out. I'm trying to have a file upload form inside a modal box,
> > > but although the form works fine when not in a modal box and just in a
> > > ctp file, when it's in the modal box it doesn't submit at all.
>
> > > I'm using Uploader by Miles Johnsonhttp://milesj.me/code/cakephp/uploader#script
> > > and the modal box is fromhttp://nyromodal.nyrodev.com/
>
> > > The code I'm using to create the form is:
> > > <a href="#test" class="nyroModal"><?php echo $this->Html-
> > >> image('uploadimage.gif');?></a>
> > > <div id="test" style="display: none; width: 600px;">    <?php
> > >    echo $form->create('Image', array('action'=>'upload', 'type' =>
> > > 'file'));
> > >    echo $form->input('fileName', array('type' => 'file'));
> > >    echo $form->end('Upload');
> > > </div>
>
> > > When I click the image the modal box pops up as expected with the form
> > > inside just like it's meant to be. But when I click submit nothing
> > > happens.
>
> > > Like I said, the exact same form works outside the modal box. Plus the
> > > outputted code appears the same:
> > > <form id="ImageUploadForm" enctype="multipart/form-data" method="post"
> > > action="/sponster/images/upload" accept-charset="utf-8">
> > >    <div style="display:none;">
> > >        <input type="hidden" name="_method" value="POST" />
> > >    </div>
> > >    <div class="input file">
> > >        <label for="ImageFileName">File Name</label>
> > >        <input type="file" name="data[Image][fileName]"
> > > id="ImageFileName" />
> > >    </div>
> > >    <div class="submit"><input type="submit" value="Upload" />
> > >    </div>
> > > </form>
>
> > > I've obviously missed something stupid or something? Can anyone help
> > > me please?
>
> > I suspect your js trying to do something like $('#ImageUploadForm').submit(function(){
>
> > // do something
>
> > };
>
> > this is fine in outside the modal as the form loads before the document is ready so js can hang an event off the dom object. When you load this in a modal, the js isnt aware of the object so no event can be triggered.
>
> > A way round this is to use the .live event handler in jquery.
>
> > $('#ImageUploadForm').live('submit',function(){
>
> > // do something
>
> > };
>
> > Also if your js is being loaded as part of your view, then its likely that in the modal its not being loaded at all. use firebug to check this. if its not loading, stick it in your main template.
>
> > Hopefully it'll be something like this.
>
> > mikek
>
> > --
> > Mike Karthäuser
> > Managing Director - Brightstorm Ltd
> > Email: mi...@brightstorm.co.uk
> > Web:http://www.brightstorm.co.uk
> > Tel: 07939 252144
> > Address: 1 Brewery Court, North Street, Bristol, BS3 1JS

--
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
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: