0

I am using AJAX and I'm getting the data from the database as Javascript variables. Now, in a table, I want to print an image using php base_url. The problem is the image is not showing. Here is my code: everything is printing except the picture

<script>
    $(function(){
       showAllEmployee();
        function showAllEmployee(){
        $.ajax({
                type: 'ajax',
                url: '<?php echo site_url('Welcome/showAllEmployee') ?>',
                async: false,
                dataType: 'json',
                success: function(data){
                                var html = '';
                    var i;

                    for(i=0; i<data.length; i++){


                        html +='<tr>'+
                                    '<td> <img class="size1"  src="<?php echo base_url("/uploads/university/data[i].image")?>" /></td>'+

                                    '<td>'+data[i].name+'</td>'+
                                    '<td>'+data[i].division+'</td>'+
                                    '<td>'+data[i].location+'</td>'+
                                    '<td>'+data[i].min_hsc+'</td>'+
                                    '<td>'+data[i].ad+'</td>'+
                                    '<td>'+data[i].email+'</td>'+
                                '</tr>';
                    }
                    $('#showdata').html(html);
                },
                error: function(){
                    alert('Could not get Data from Database');
                }
            });
        }
        }
        )
        </script> 
4
  • just move that out of the php block. Commented Aug 24, 2017 at 20:00
  • or when you return the data, return the whole url. Commented Aug 24, 2017 at 20:01
  • Can you show your image url when it is printed on a page? You can verify this using firebug or developer tools in any popular web browser. Commented Aug 24, 2017 at 20:01
  • '<td> <img class="size1" src="<?php echo base_url("/uploads/university/")?>data[i].image" /></td>'+ wrote like that, but not working Commented Aug 24, 2017 at 20:14

2 Answers 2

1

I'd say something like this:

'<td> <img class="size1" src="<?php echo base_url("/uploads/university/")?>' + data[i].image + '"/></td>'

Sign up to request clarification or add additional context in comments.

Comments

0

You have not use javascript code directly with php. Please see below code. I hope it will works for you.

for(i=0; i<data.length; i++){
       document.cookie = "image =" + data[i].image;
     <?php
       $image= @$_COOKIE['image'];
     ?> 
                    html +='<tr>'+
                                '<td> <img class="size1"  src="<?php echo base_url("/uploads/university/".$image)?>" /></td>'+

                                '<td>'+data[i].name+'</td>'+
                                '<td>'+data[i].division+'</td>'+
                                '<td>'+data[i].location+'</td>'+
                                '<td>'+data[i].min_hsc+'</td>'+
                                '<td>'+data[i].ad+'</td>'+
                                '<td>'+data[i].email+'</td>'+
                            '</tr>';
                }

1 Comment

@shawn Check your console and find what error occured? and let me know!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.