0

I am trying to get jQuery to change the html of an element and include PHP. It all works fine apart from PHP. Here is what I have:

$('.gridheader').each(function(index){
    $(this).toggle(function (){
        $('.gridimage:eq('+index+'), .gridinfo:eq('+index+')').slideToggle();
        $('.gridheadinfo:eq('+index+')').html('Click to close <img src="<?php echo base_url() ?>/assets/images/closetab.png" width="10" height="10" />');
    }, function(){
        $('.gridimage:eq('+index+'), .gridinfo:eq('+index+')').slideToggle();
        $('.gridheadinfo:eq('+index+')').html('Click for info <img src="<?php echo base_url() ?>/assets/images/scrollup.png" width="11" height="10" />');
    });
});

I have the image originally in there (HTML):

<div class="float-left">Kredible</div><div class="float-right gridheadinfo">Click for info <img src="<?php echo base_url() ?>assets/images/scrollup.png" width="11" height="10" /></div></div>

The HTML works fine, however when I change it using jQuery the image doesn't show. Ive looked at it in the inspector after the change and the PHP shows up as PHP instead of converting to HTML?

Im rather puzzled.

Thanks.

3
  • 2
    That would be because PHP is executed server side, while javascript is changing the HTML directly in the clients browser Commented Aug 19, 2012 at 23:33
  • Hmn makes sense... any hints on how I can get around this? Commented Aug 19, 2012 at 23:36
  • 2
    Generate an element that holds reference to the PHP variable on page load. <input type="hidden" id="baseURL" name="baseURL" value="<?php echo base_url(); ?>" />. Then you can reference it in the HTML with <img src="'+$('#baseURL').val()+'/assets/images/closetab.png" width="10" height="10"/> Commented Aug 19, 2012 at 23:37

2 Answers 2

2

Save your server-side variable and then use it:

      var url="<?php echo base_url() ?>"; 
    //this will be parsed by the php interpreter at page load so url will be "localhost/subdomain/" probably. 

        $('.gridheader').each(function(index){
            $(this).toggle(function (){
                $('.gridimage:eq('+index+'), .gridinfo:eq('+index+')').slideToggle();
                $('.gridheadinfo:eq('+index+')').html('Click to close <img src="'+url+'/assets/images/closetab.png" width="10" height="10" />');
    //use it here
            }, function(){
                $('.gridimage:eq('+index+'), .gridinfo:eq('+index+')').slideToggle();
                $('.gridheadinfo:eq('+index+')').html('Click for info <img src="'+url+'/assets/images/scrollup.png" width="11" height="10" />');
            });
        });
Sign up to request clarification or add additional context in comments.

Comments

0

Including a php file only influences the html while being on the server.

Thus, the changed code won't have any affect, because the php script is not executed.

You could load the content of a html file and than put it into your page. SQL would not be possible from within that, of course.

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.