Kode Creations
Kode Creations

Reputation: 3

Include PHP in jQuery

I am trying to include some PHP inside a piece of jQuery code.

The PHP pulls a static block from within WordPress and I need a way to add this into the jQuery. Each time I add it in the block either doesn't show at all or the contents don't show.

The code which gets the PHP is

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('cat_block')->toHtml() ?>

And the jQuery is

jQuery(document).ready(function () {
jQuery(".catalog-category-view .last li.item:nth-child(7)").after("<li class='category-block-call'>PHP NEEDS TO GO HERE</li>");
});

As you can see I need to get the PHP code or the contents of the PHP code inbetweeen the li tags in the jQuery.

Upvotes: 0

Views: 192

Answers (5)

Abdul Bashet
Abdul Bashet

Reputation: 1

If your file is able to interpret php code, than below code should work.

jQuery(document).ready(function () {
    jQuery(".catalog-category-view .last li.item:nth-child(7)").after("<li class='category-block-call'><?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('cat_block')->toHtml() ?></li>");
});

Upvotes: 0

Paarth
Paarth

Reputation: 10377

You can't insert PHP code with JQuery because PHP is meant to run on the server and JQuery is meant to run on the client. You're going to have to get the server to run that code somehow and then you can use the results inside of wherever you want to put it.

You could do the oldschool approach and create an invisible iframe to the php page on your server. You can use JQuery to rip the contents out of there and put them in the proper place.

Alternatively, you could rewrite it a bit so that the server sends an AJAX response.

Upvotes: 1

nubinub
nubinub

Reputation: 1938

I think the only way to access server side code from you jquery function is to make an ajax call.

Upvotes: 1

Manoj Yadav
Manoj Yadav

Reputation: 6612

Try this (Blow code need to be in PHP file):

<script>
jQuery(document).ready(function() {
    jQuery(".catalog-category-view .last li.item:nth-child(7)").after("<li class='category-block-call'><?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('cat_block')->toHtml(); ?></li>");
});
</script>

Upvotes: -1

Tdelang
Tdelang

Reputation: 1308

You have to execute the php on the server side. You're going to have to use ajax ($.ajax()) if you want to get that information without a page refresh.

Upvotes: 1

Related Questions