How To Insert Php Into Jquery?
Solution 1:
First, you should wrap js-code with script
-tag:
<!-- Wordpress template URL --><divid="template_directory"style="display: none;"><?php bloginfo('template_url'); ?></div><!-- Fetch with jQuery --><scripttype="text/javascript">var templDirec = $("#template_directory").html();
var imgHTML = '<img src="' + templDirec + '/images/my-image.jpg" />';
</script>
If you want to pass some data from PHP to JS use echo
:
<scripttype="text/javascript">var templDirec = $("#template_directory").html();
var imgHTML = '<img src="<?phpecho$image?>" />';
</script>
where $image
-php variable. In general you may generate any JS code using PHP as well as html.
To pass data from JS to PHP (from client to server) use AJAX
I hope this will help.
Solution 2:
Your question is very ambigious, its not clear what you are asking. Please edit and refine your question as soon as you learn something from a comment or an answer.
First of all, as poined out by webdeskil in comments, you can indeed call PHP code from client side and in fact that is done very often. What you can not do is executing PHP code "ON" the client side. Any request for executing server side code, in this case PHP has to go to the server where it can, and it makes sense for it, to be executed.
From your tags it looks like you are writing some code for wordpress. IMHO a good way of learning programming skills is to look at existing code. Since wordpress is opensource, you have the luxury of being able to open any plugin or theme and take a look inside. However, the number one resource for your wordpress needs is the codex of course. Read the codex document carefully, in this case codex document for using javascript is relevant to your question.
Some tips that maybe helpful:
- You should avoid writing javascript file where php can do the same thing
- If you use JQuery, you should know that wordpress already loads an instance of JQuery for you and you should avoid loading another instance if you can
Anyway, to give you any more help you need to edit and refine your question further, otherwise its all guesswork for us trying to help.
Post a Comment for "How To Insert Php Into Jquery?"