简体   繁体   中英

How to add same javascript multiple times?

I'm using codeigniter.I need to add same java-script multiple,reason is i us 3 file upload button to upload.to save data i use java-scrip plugin photos this is my view php file

pic1 PIC1

在此输入图像描述 pic2

button 2(pic2) is working properly but button1(pic1) is not because same JavaScript is used this is the JavaScript.how can

  <script src="<?php echo $this->config->item('base_url'); ?>upload_assets/jquery.uploadify.min.js" type="text/javascript"></script>
<!--carbuddy photos upload--> 
  <script>


        <?php $timestamp = time();?>
        //javascript for file upload 1  
        $(function() {
                $('#package_file_upload1').uploadify({
                                'method'   : 'post',
                'formData' : {
                            'timestamp' : '<?php echo $timestamp;?>',
                                    'token'     : '<?php echo md5('unique_salt' . $timestamp);?>',
                                        'filetype' : '7',
                                        'id'       :<?php echo $usermetadata['ProfileID'];?>,

                        },
                        'removeTimeout' : 3,
                        'progressData' : 'speed',
                        'buttonImage' : '<?php echo $this->config->item('base_url'); ?>upload_assets/browse-btn.png',
                        'swf'      : '<?php echo $this->config->item('base_url'); ?>upload_assets/uploadify.swf',
                        'uploader' : '<?php echo $this->config->item('base_url'); ?>photo/image_upload/upload',
                         'onUploadSuccess' : function(file, data, response) {
                            window.location.reload();  
                             var obj = jQuery.parseJSON(data);
             if(obj.status == 1)
             {
                var img = obj.path+obj.name;
                var photoid = obj.id;
                if(photoid > 0){
                $('#car_stream').append('<li id="photo_'+photoid+'" class="span3" ><i class="icon-remove-sign" photo="'+photoid+'" style="cursor:pointer;position: relative; float: right; top: 4px; right: 20px;"></i><a class="thumbnail fancybox-button zoomer" data-rel="fancybox-button" title="" href="'+img+'"><div style="height: 150px;overflow: hidden;"><img style="width: 220px;" src="'+img+'" alt=""></div></a></li>');
                }
             }
             else
             {
                 alert(obj.msg);
             }
                   }
                });
        });




        //set tour buddy host mode 
$('#becarbuddy').click(function(){
    var CarMode = 0;
      if ($('#becarbuddy').is(':checked')) {
          CarMode = 1;
            }
      $.ajax({
            type:"POST",
            data:{CarMode:CarMode},
            url:'<?php echo base_url();?>host/host_cont/ajax_update_host_mode', 
            success:function(response){
            }
    });
});
</script>

same JavaScript(only different is 'filetype' : '5' and #package_file_upload12 in button1(pic1) reason is by filetype system will identify where need to be save whether car photo or profile pic photo ) code is paste and i try.but i didn't work properly how can i fix ths error and if there is way do id JavaScript. need a quick help

this is the HTML code

<input id="package_file_upload1" class="form-control input-sm" placeholder="Car picture"  name="file[]" type="file" multiple="true" >

<input id="package_file_upload12" class="form-control input-sm" placeholder="profile picture"  name="file[]" type="file" multiple="true" >

In your each input, add a class named upload-btn :

<input id="package_file_upload1" class="form-control input-sm upload-btn" placeholder="Car picture"  name="file[]" type="file" multiple="true" >

<input id="package_file_upload12" class="form-control input-sm upload-btn" placeholder="profile picture"  name="file[]" type="file" multiple="true" >

In your javascript, replace to this code:

 //each is a function that will loop through and apply all the following
 //code to the element with class upload-btn
 $('.upload-btn').each(function () {
     //we are going to determine the file type by input 'id'
     element_id = $(this).attr('id');
     var type_of_file = '';
     if (element_id == 'package_file_upload1') {
         type_of_file = '7'; //file type for car
     } else if (element_id == 'package_file_upload12') {
         type_of_file = '5'; //file type for profile
     }

     //the uploadify method
     $(this).uploadify({
         'method': 'post',
         'formData': {
             'timestamp': '<?php echo $timestamp;?>',
             'token': '<?php echo md5('
             unique_salt ' . $timestamp);?>',
             'filetype': type_of_file, //dynamic file type
             'id': <?php echo $usermetadata['ProfileID']; ?> ,

         },
        //...rest of your code follows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM