Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

jaguar15

Programmer
Jun 10, 2015
3
0
0
NL
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="[URL unfurl="true"]https://www.dropbox.com/static/api/2/dropins.js"[/URL] 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>
 
well firstly you don't need a global counter to just add another entry to an array.

Have a look at the 'push' command
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 : [highlight #FCE94F]a[/highlight], filename : [highlight #FCE94F]b[/highlight] });

function saver(){
var options = { 

//here i want to use array i created above instead of hardcode filepath and filenames
[highlight #FCE94F]
files: userFiles[/highlight],

...

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top