Wednesday, September 28, 2011

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

Hi Sarah

On 28 Sep 2011, at 15:05, crazysarahtempleton@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 Johnson http://milesj.me/code/cakephp/uploader#script
and the modal box is from http://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: mikek@brightstorm.co.uk 
Web: http://www.brightstorm.co.uk 
Tel: 07939 252144
Address: 1 Brewery Court, North Street, Bristol, BS3 1JS

No comments: