Upload in modal window and pass values with jQuery
Do you publish your blogs with WordPress? If yes, you know the inline popup window which opens if you like to upload / insert an image into your article. This tutorial shows you how-to use the jQuery Plugin ThickBox and some jQuery code to upload a file and pass a value to the parent page. You can use this kind of feature in your custom CMS or maybe you like use it as a basic for your own file upload plugin or gadget?
Scripts and libraries used in this tutorial
PHP Upload Class / Script
We use this PHP Script written by our partner finalwebsites.com because it’s very powerful and easy to use. Since the upload process is not the difficult part in this tutorial you might use your own PHP upload code instead.
jQuery JavaScript Library
There are several client side libraries available on the Internet, I like jQuery because there are many resources and cool plugins.
ThickBox – webpage UI dialog widget
I tried several Lightbox types and since a while I use the ThickBox plugin. For the upload process, we need modal window which will open as an Iframe.
Our dynamic form (form.php)
Most forms process some data for a database or they send the data within an e-mail message. In our example we have a form with just two form fields one for a name tag and one for the file name.
<form action="form.php" method="post"> <p> <label for="nametag">Name</label> <input type="text" name="nametag" size="20" /> </p> <p> <label for="myfile">File name</label> <input type ="text" id="myfile" name="myfile" readonly="readonly" /> <a href="upload.php?placeValuesBeforeTB_=savedValues&TB_iframe=true&height=300&width=400&modal=true" class="thickbox">Upload</a> </p> <p style="text-align:right;"><input type="submit" name="Submit" value="Send" /></p> </form>
Notice the attributes and the URL used inside the link element to our upload script. We set the ThickBox size, we enable the values for iframe and modal. We used also some ID for the “readonly” form field which is used to store the filename after the upload is done. We added also a form action because we need to process the form afterwards.
The upload script (upload.php)
After the user has clicked the upload link, a lightbox will open an Iframe holding the upload script. First we show the code which is responsible for the upload process.
include_once $_SERVER['DOCUMENT_ROOT'].'/pathtotheclass/upload_class.php'; $error = ''; $img = ''; $copy_link = ''; if(isset($_POST['Submit'])) { $my_upload = new file_upload; $my_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/modal-upload/files/"; $my_upload->extensions = array(".png", ".jpg", ".gif"); // allowed extensions $my_upload->rename_file = true; $my_upload->the_temp_file = $_FILES['upload']['tmp_name']; $my_upload->the_file = $_FILES['upload']['name']; $my_upload->http_error = $_FILES['upload']['error']; if ($my_upload->upload()) { $image = $my_upload->file_copy; $copy_link = ' | <a id="closelink" href="#" onclick="self.parent.tb_remove();">Pass file name</a>'; } $error = $my_upload->show_error_string(); }
You will find several example files if you download the upload class from finalwebsites. For this tutorial we will explain the most important variables and methods.
$my_upload->upload_dir
This upload directory is important, while most webhosts don’t allow that Apache has write access to directories, you need to chmod the directory with 777. If this directory doesn’t exists the upload will fail.
$my_upload->extensions
Allow only the extensions you need in your application, in our example we need to retrieve the image’s filename.
$my_upload->rename_file
Your users might have file names with spaces or foreign characters, use this option to rename your uploaded file to an URL-safe string.
$my_upload->file_copy
The (new) name generated by a successful upload, use this name in your further application process.
Let’s check the upload form:
<form action="upload.php" method="post" enctype="multipart/form-data"> <p> <label for="upload">Select file</label> <input name="upload" type="file" size="15" /> <br /> <input type="submit" name="Submit" value="Upload" /> </p> </form> <p><?php echo $error; ?></p> <p><a href="#" onclick="self.parent.tb_remove();">Cancel</a><?php echo $copy_link; ?></p>
Because this is an upload form, you need add the “enctype” attribute “multipart/form-data”, otherwise the file will not be uploaded to your server. You see two link elements below the form, one is static to cancel/close the upload dialog and the other one is generated by PHP after the upload is done and gets the ID “closelink”. This ID is important for the following jQuery function:
$(document).ready(function() { $("#closelink").click(function() { $("#myfile", top.document).val('<?php echo $image; ?>'); }); });
If the user clicks the link with the ID “closelink”, the filename is passed to top document’s element with the ID “myfile” and the ThickBox gets closed. With the completed form we’re able to process the data inside the parent document. We added this php upload demo to our partners site, which will demonstrate the “modal effect”. If you like to use the files from this tutorial, you can download the “form.php” and “upload.php” files from the php forum. If you have questions or suggestions, please post them them below.
Related posts
- Upload images for usage in TinyMCE
- Tutorial: FTP Upload via cURL
- jQuery form plugin and PHP file uploads
Comments
Trackback URL for this post: http://www.web-development-blog.com/archives/upload-in-modal-window-and-pass-values-with-jquery/trackback/
Sorry, the comment form is closed at this time.












[...] 12)Upload in modal window and pass values with jQuery This PHP / jQuery tutorial explains how-to upload a file PHP from ThickBox and how after successful upload the filename is passed to the parent document. The function is a useful addition for custom content management systems. [...]