0

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

enter image description here 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" >
4
  • On first look, why do you have two inputs with same id? If you need to refer to them together, put it under class instead. Commented Mar 26, 2014 at 9:38
  • ahhh sorry i put unedited code i change it minute ago but same result.. Commented Mar 26, 2014 at 9:44
  • Not very clear, but do you mean the contents inside $('#package_file_upload1').uploadify({ is the same for all three buttons so you want to have it written just once but working for all three buttons? Commented Mar 26, 2014 at 9:49
  • almost same only different is "'filetype' : 7" different button have different number filetype number because upload photos are goes to different location. Commented Mar 26, 2014 at 9:56

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.