INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to append/save pairs of data into dynamic javascript array and pass it to second function?

How to append/save pairs of data into dynamic javascript array and pass it to second function?

How to append/save pairs of data into dynamic javascript array and pass it to second function?

(OP)
I am trying to convert the following hardcoded pairs of data to dynamic javascript array and use that array in saver function?

CODE -->

files: [

        {'url': '<?PHP  echo $imagePath1_Value; ?>', 'filename': '1.jpg'},
        {'url': '<?PHP  echo $imagePath2_Value; ?>', 'filename': '2.jpg'},
        {'url': '<?PHP  echo $imagePath3_Value; ?>', 'filename': '3.jpg'},
    ], 

i tried the following but saver function keep giving me this error:Missing files.See Documentation.so that means either i didnt save data pairs to array correctly or i didn't pass the array to saver function correctly!could any one help me fix this problem and save pairs of data to array and pass it to saver function?

CODE

<script type="text/javascript" src="https://www.dropbox.com/static/api/2/dropins.js" id="dropboxjs" data-app-key="xxxxxxxxxxxxxx"></script>
<script>
    var i=1;
    files = new Array();

        function addtoArray(a,b){
        alert("URL:"+a+"\nFileName:"+b);

        files[i] = { url: +a, filename: +b };
        i++;

        };

function saver(){
var options = { 

//here i want to use array i created above instead of hardcode filepath and filenames

files:[];

//files: [
    
        //{'url': '<?PHP  echo $imagePath1_Value; ?>', 'filename': '1.jpg'},
        //{'url': '<?PHP  echo $imagePath2_Value; ?>', 'filename': '2.jpg'},
        //{'url': '<?PHP  echo $imagePath3_Value; ?>', 'filename': '3.jpg'},
    //],


    success: function () {
        // Indicate to the user that the files have been saved.
        alert("Success! Files saved to your Dropbox.");
    },


    progress: function (progress) {},


    cancel: function () {},


error:function (errorMessage) { alert("ERROR: " + errorMessage); }
};

Dropbox.save(options);

};
</script>    
    <body>
    <button onclick="addtoArray('<?PHP  echo $imagePath1_Value; ?>','1.jpg')">add to array</button>

    <button onclick="addtoArray('<?PHP  echo $imagePath2_Value; ?>','2.jpg')">add to array</button>

    <button onclick="addtoArray('<?PHP  echo $imagePath3_Value; ?>','3.jpg')">add to array</button>
    <button onclick="saver()">save</button> 

RE: How to append/save pairs of data into dynamic javascript array and pass it to second function?

well firstly you don't need a global counter to just add another entry to an array.

Have a look at the 'push' command http://www.w3schools.com/jsref/jsref_push.asp

secondly when you add the JSON object to the array, you don't use the + sign on the input arguments (this isn't concatenation or mathematical addition).

I'd also change your array name to not conflict with the key in the options JSON for the dropbox method.

CODE

var userFiles = [];

userFiles.push({ url : a, filename : b });

function saver(){
var options = { 

//here i want to use array i created above instead of hardcode filepath and filenames

files: userFiles,

... 

untested but something like that.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close