SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to pass a variable dynmically

    I'm trying to overcome one feat, and I've just ran the course of my little knowledge of javascript. Here is the code that concerns us.

    Code:
    function loadXMLDoc()
    {
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        populateParentWindow(xmlhttp.responseText);
        }
      }
    xmlhttp.open("GET","https://www.syncupsolutions.net/currentdirectory-<?php echo $_COOKIE['User'] ?>.txt",true);
    xmlhttp.send();
    }
    function populateParentWindow(Directory) {
    
    		 
         $("#FTPContents").load('functions/testftp.php?HASH=' + Get_Cookie("ftphash") + "&reload=" + Directory) ;
    	 };
    $(function(){	
    	 
    	$('#swfupload-control').swfupload({
    		upload_url: 'functions/upload-file.php?UID=' + Get_Cookie("User"),
    		file_post_name: 'uploadfile',
    		file_size_limit : "10000",
    		file_types : "*.*",
    		file_types_description : "All Files",
    		file_upload_limit : 5,
    		flash_url : "functions/js/swfupload/swfupload.swf",
    		button_image_url : 'functions/js/swfupload/wdp_buttons_upload_114x29.png',
    		button_width : 114,
    		button_height : 29,
    		button_placeholder : $('#button')[0],
    		debug: false
    	})
    		.bind('fileQueued', function(event, file){
    			var listitem='<li id="'+file.id+'" >'+
    				'File: <em>'+file.name+'</em> ('+Math.round(file.size/1024)+' KB) <span class="progressvalue" ></span>'+
    				'<div class="progressbar" ><div class="progress" ></div></div>'+
    				'<p class="status" >Pending</p>'+
    				'<span class="cancel" >&nbsp;</span>'+
    				'</li>';
    			$('#log').append(listitem);
    			$('li#'+file.id+' .cancel').bind('click', function(){
    				var swfu = $.swfupload.getInstance('#swfupload-control');
    				swfu.cancelUpload(file.id);
    				$('li#'+file.id).slideUp('fast');
    			});
    			// start the upload since it's queued
    			$(this).swfupload('startUpload');
    		})
    		.bind('fileQueueError', function(event, file, errorCode, message){
    			alert('Size of the file '+file.name+' is greater than limit');
    		})
    		.bind('fileDialogComplete', function(event, numFilesSelected, numFilesQueued){
    			$('#queuestatus').text('Files Selected: '+numFilesSelected+' / Queued Files: '+numFilesQueued);
    		})
    		.bind('uploadStart', function(event, file){
    			$('#log li#'+file.id).find('p.status').text('Uploading...');
    			$('#log li#'+file.id).find('span.progressvalue').text('0%');
    			$('#log li#'+file.id).find('span.cancel').show();
    		})
    		.bind('uploadProgress', function(event, file, bytesLoaded){
    			//Show Progress
    			var percentage=Math.round((bytesLoaded/file.size)*100);
    			$('#log li#'+file.id).find('div.progress').css('width', percentage+'%');
    			$('#log li#'+file.id).find('span.progressvalue').text(percentage+'%');
    		})
    		.bind('uploadSuccess', function(event, file, serverData){
    			var item=$('#log li#'+file.id);
    			item.find('div.progress').css('width', '100%');
    			item.find('span.progressvalue').text('100%');
    			var pathtofile='<a href="uploads/'+file.name+'" target="_blank" >view &raquo;</a>';
    			item.addClass('success').find('p.status').html('Done!!! | '+pathtofile);
    		})
    		.bind('uploadComplete', function(event, file){
    			// upload has completed, try the next one in the queue
    			$(this).swfupload('startUpload');
    			loadXMLDoc();
    			
    		})
    You'll notice at the beginning, the functino loadXMLDoc.

    This function GETS the contents of a text file, which is the path to the directory the user is browsing.

    The next function is populateParentWindow. This is the function that dynamically updates the div that displays the files and folders of the current directory so they can see their file has been uploaded.

    Scroll down a little further and you'll see my problem.

    Code:
    $(function(){	
    	 
    	$('#swfupload-control').swfupload({
    		upload_url: 'functions/upload-file.php?UID=' + Get_Cookie("User"),
    At the bottom of the above line, the :
    upload_url, I need it to contain the contents of the text file also, but in a POST value, like the UID, something like, directory=thedirectory.

    But, when I try
    Code:
    $(function(){	
    	 
    	$('#swfupload-control').swfupload({
    		upload_url: 'functions/upload-file.php?UID=' + Get_Cookie("User") + "&directory=" + xmlhttp.responseText,
    It gives me an undefined, right off the bat before any uploading, or anything at all takes place. I'm guessing this is because this value is set on page load, and not before the actual upload actually happens.

    Does anyone have any ideas how I can set this value when the upload begins with the variable from the text file ? Keep in mind it has to be inside the function it's in now or it wont work.

    Note that the loadXMLDocument function isn't called until the upload is complete, the .bind at the bottom of the page.

    I've tried pulling it from the text file several ways, setting the document.title to the current directory and calling it in the function, it just always seems a step behind me or undefined. Any ideas ?

  2. #2
    SitePoint Enthusiast ironikart's Avatar
    Join Date
    Apr 2005
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there any reason the swfupload portion of your code is placed inside a self executing function? Perhaps if the variable in question isn't set, waiting until the document is loaded might help:

    Code JavaScript:
    $(document).ready(function(){
    // Your swfupload calls here
    });

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ironikart View Post
    Is there any reason the swfupload portion of your code is placed inside a self executing function? Perhaps if the variable in question isn't set, waiting until the document is loaded might help:

    Code JavaScript:
    $(document).ready(function(){
    // Your swfupload calls here
    });
    Yes, that's to wait until the document is loaded.

    You can also use the jQuery callback

    Code javascript:
    $(function() {
        // code here
    });

    And if you want to be sensitive towards noConflict situations, you can have jQuery pass the $ object in to the function


    Code javascript:
    jQuery(function($) {
        // code here
    });
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Enthusiast ironikart's Avatar
    Join Date
    Apr 2005
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Yes, that's to wait until the document is loaded.

    You can also use the jQuery callback

    Code javascript:
    $(function() {
        // code here
    });
    My bad, I misread that.

  5. #5
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    The place in the code where you're trying to use xmlhttp.responseText :

    Code JavaScript:
    upload_url: 
       'functions/upload-file.php?UID=' + Get_Cookie("User") + 
       "&directory=" + xmlhttp.responseText,

    Probably won't work because you are trying to use the value of the response of the completed XHR request.

    Do you know if swfupload() has a way of setting the "upload_url" option *after* the first implementation? (without having to instantiate an entirely new swfupload)
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }


Bookmarks

Posting Permissions

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