www.webdeveloper.com
Results 1 to 6 of 6

Thread: Clone file input field and insert into database

  1. #1
    Join Date
    Nov 2009
    Location
    Netherlands
    Posts
    21

    Clone file input field and insert into database

    Hello all,

    First my english sucks, but I don't think you all can read dutch

    OK, i'm busy making a site for a audiovisual company and i'm stuck at the CMS system by adding pictures to a project page.

    Adding one single photo isn't the problem but I want a button to add a new file input field and insert that second (or thirth, fourth etc etc) into a new row in the database....

    The script that i'm using right now, clones the input field but doesn't "activates" it.... You can't select a file....

    The javascript:
    Code:
    	
    function clone() {
    		var parent=document.getElementById('naw0');
    		var cloned=parent.cloneNode(true);
    		// unique id
    		var aNaam=document.getElementsByName('imgfile[]');
    		var num=aNaam.length;
    		cloned.id='naw'+num;
    		//insert after last pair
    		parent.parentNode.insertBefore(cloned, document.getElementById('naw'+(num-1)).nextSibling);
    		//clear inputs
    		var aInput=cloned.parentNode.getElementsByTagName("file");
    		aInput[aInput.length-3].value='';
    
    	}
    and the HTML/PHP code:


    PHP Code:
    <?php
    if (isset($_REQUEST['submit'])) { 
        
    $name $_POST["name"];
        
    $description $_POST["description"];
        
    $video $_POST["video"];
        
        for (
    $i=0$i count($Naam); $i++)
        {
            
    //foto uploaden
        
                
    $path_thumbs "upload/thumbs";
                
    $path_big "upload/images";
                
                
    //the new width of the resized image.
                    
    $img_thumb_width 150// in pixel
                //Do you want to limit the extensions of files uploaded (yes/no)
                    
    $extlimit "no"
                
    //allowed Extensions
                    
    $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
                
    $file_type $_FILES['imgfile[]']['type'];
                
    $file_name $_FILES['imgfile[]']['name'];
                
    $file_size $_FILES['imgfile[]']['size'];
                
    $file_tmp $_FILES['imgfile[]']['tmp_name'];
        
               
    //check file extension
               
    $ext strrchr($file_name,'.');
               
    $ext strtolower($ext);
               if ((
    $extlimit == "yes") && (!in_array($ext,$limitedext))) {
                  echo 
    "Verkeerde extensie.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
                  exit();
               }
               
    //get the file extension.
               //$getExt = explode ('.', $file_name);
               //$file_ext = $getExt[count($getExt)-1];
               
               
    $file_ext end(explode("."$file_name));
        
               
    //create a random file name
               
    $rand_name md5(time());
               
    $rand_namerand(0,999999999);
               
    //get the new width variable.
               
    $ThumbWidth $img_thumb_width;
        
               
    //keep image type
               
    if($file_size){
                  if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){
                       
    $new_img imagecreatefromjpeg($file_tmp);
                   }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){
                       
    $new_img imagecreatefrompng($file_tmp);
                   }elseif(
    $file_type == "image/gif"){
                       
    $new_img imagecreatefromgif($file_tmp);
                   }
                   
    //list width and height and keep height ratio.
                   
    list($width$height) = getimagesize($file_tmp);
                   
    $imgratio=$width/$height;
                   if (
    $imgratio>1){
                      
    $newwidth $ThumbWidth;
                      
    $newheight $ThumbWidth/$imgratio;
                   }else{
                         
    $newheight $ThumbWidth;
                         
    $newwidth $ThumbWidth*$imgratio;
                   }
                   
    //function for resize image.
                   
    if (function_exists(imagecreatetruecolor)){
                   
    $resized_img imagecreatetruecolor($newwidth,$newheight);
                   }else{
                         die(
    "Error: Please make sure you have GD library ver 2+");
                   }
                   
    imagecopyresized($resized_img$new_img0000$newwidth$newheight$width$height);
                   
    //save image
                   
    ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
                   
    ImageDestroy ($resized_img);
                   
    ImageDestroy ($new_img);
                   
    move_uploaded_file ($file_tmp"$path_big/$rand_name.$file_ext");
                   
                }
        


        
    //foto's toevoegen
            
    $sql2 "INSERT INTO images (link,thumb,name) VALUES ('$path_big/$rand_name.$file_ext','$path_thumbs/$rand_name.$file_ext','$name')";
        }
            
    //algemene data invoegen
            
    $sql "INSERT INTO projecten (name,description,video) VALUES ('$name','$description','$video')";
            
            if( (
    $result mysql_query($sql)) && ($result2 mysql_query($sql2)) ) { 
                echo 
    '<p><img src="images/icons/accept.gif" alt"" /> Portfolio item succesvol aangemaakt!</p>'
                
    $page "index.php?page=portfolio";
                
    $sec "1";
                
    header("Refresh: $sec; url=$page");
            } else { 
                echo 
    "ERROR: ".mysql_error(); 
            } 
    } else { 
    ?>
    HTML Code:
    <div id="container">
    
    <form action="" enctype="multipart/form-data" method="post" class="niceform" name="UD">
    
    <dl>
    	<dt><label for="name">Titel:</label></dt>
        <dd><input type="text" name="name" id="name" size="32" maxlength="128" /></dd>
    </dl>
    <dl>
    	<dt><label for="description">Beschrijving:</label></dt>
    	<dd><textarea name="description" id="description" rows="8" cols="65"></textarea></dd>
    </dl>
    <div id="naw0">
    	<dl>
    		<dt><label for="imgfile">Kies foto:</label></dt>
    		<dd><input name="imgfile[]" id="imgfile" type="file" /></dd>
    	</dl>
    </div>
    <dl>
    	<dt><label for="video">(Embed) Video:</label></dt>
    	<dd><textarea name="video" id="video" rows="8" cols="65"></textarea></dd>
    </dl>
    		<dd><input type="submit" name="submit" id="submit" value="voeg toe" onClick="return validateForm()" /></dd>
    		<dd><button type="button" onclick="clone();">Nog een afbeelding</button></dd>
    
    </form>
    </div>
    </fieldset>
    
    <?php
    }
    ?>

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    id must be unique
    Code:
    cloned.id='naw'+num;
    var newValue = 'imgfile'+num;
    cloned.getElementsByTagName('label')[0].setAttribute('for', newValue);
    cloned.getElementsByTagName('input')[0].id = newValue;
    //insert after last pair
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Nov 2009
    Location
    Netherlands
    Posts
    21
    It's still not active, when I add a new input field.....

    Code:
    	function clone() {
    		var parent=document.getElementById('naw0');
    		var cloned=parent.cloneNode(true);
    		// unique id
    		var aNaam=document.getElementsByName('imgfile[]');
    		var num=aNaam.length;
    		cloned.id='naw'+num;
    		//id must be unique
    		var newValue = 'imgfile'+num;
    		cloned.getElementsByTagName('label')[0].setAttribute('for', newValue);
    		cloned.getElementsByTagName('input')[0].id = newValue;
    		//insert after last pair
    		parent.parentNode.insertBefore(cloned, document.getElementById('naw'+(num-1)).nextSibling);
    		//clear inputs
    		var aInput=cloned.parentNode.getElementsByTagName("file");
    		aInput[aInput.length-3].value='';
    
    	}

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    you have to physically click on a file upload to fill it with data.
    this is a browser-enforced security policy.
    there is no way to clone a filled input.
    even if the duped .value looks like it holds the file, there won't be any actual data.
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

  5. #5
    Join Date
    Nov 2009
    Location
    Netherlands
    Posts
    21
    No i don't want to clone a filled input field, i want a new, clear file input field so they can choose a new image to upload...

  6. #6
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Only Opera copy the value of the file input.
    Create a file input rather than cloning.

    "file" is an attribute not the tag name
    Code:
    var aInput=cloned.parentNode.getElementsByTagName("file")
    At least 98% of internet users' DNA is identical to that of chimpanzees

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.14083 seconds
  • Memory Usage 2,966KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_code
  • (1)bbcode_html
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates