1

I understand that one can't execute PHP from the client side (JavaScript), but what would then be the appropriate approach? Writing my jQuery in PHP? Here is an example of what I'm looking for.

<!-- Wordpress template URL -->
<div id="template_directory" style="display: none;">
    <?php bloginfo('template_url'); ?>
</div>

<!-- Fetch with jQuery -->
var templDirec = $("#template_directory").html();
var imgHTML = '<img src="' + templDirec + '/images/my-image.jpg" />';

Since I'm new to this, simple descriptive answers are preferred. What's the best way of having js and php work together? Thanks

5
  • 1
    What exactly are you trying to achieve? You CAN execute PHP from the client side with JavaScript using xmlHttpRequest (you may know it as AJAX). Commented Feb 26, 2012 at 7:33
  • @Michas the above works, I'm just wondering about best practices for such tasks. This is just me trying to work around this issue. Commented Feb 27, 2012 at 7:01
  • @webdeskil The above is just an example. Could you show me how to do the above with AJAX? Commented Feb 27, 2012 at 7:26
  • Yours code looks fine for me. The AJAX would be more complicated, slower and harder to maintain. Commented Feb 27, 2012 at 11:48
  • Michas - actually properly established asynchronous calls would in fact be easier to maintain, less complicated and produce faster loading pages. The caveat being the site design is such that makes use of this. @marcup - your question hinges on it's intended accomplishments which are not clear given the code structure provided. <?php $slides="right in"; ?> assuming you know what you are doing and the environment isnt full of strange, strange things. Your request for "best practices" is vague because such "best" practices are dependent on what is being delivereredededed. Commented Dec 7, 2012 at 8:17

2 Answers 2

2

First, you should wrap js-code with script-tag:

<!-- Wordpress template URL -->
<div id="template_directory" style="display: none;">
    <?php bloginfo('template_url'); ?>
</div>

<!-- Fetch with jQuery -->
<script type="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:

<script type="text/javascript">
var templDirec = $("#template_directory").html();
var imgHTML = '<img src="<?php echo $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.

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

5 Comments

$image should have a semi-colon (;) after it in the 2nd code block.
Hey Dude, Thanks but it's not what I'm looking for. I don't think that your code would work either, because I have tried something similar and all I got was a string literal of my PHP (in other words the PHP didn't convert). Also, I have my jQuery in a .js document so no <script> tag needed. How would the ajax thingy look like?
@Tarek Fadel: semi-colon is not required before cosing php-tag(?>).
@marcup: Than tell please what exactly you are looking for. What have you tried to do? and what errors have you got? Your question is not clear. I tried to roughly describe "communcation" between JS and PHP code. Something similiar doesn't mean the same. My code works. It must be in php file, not in the js. js files are not interpreted by PHP(just in case). If you want to insert js code into html or php file you must use <script>-tag. It is required. Included jQuery library doesn't affect other js on a page. Regarding AJAX. I gave you the link to the documentation. There are lots of examples.
@RuslanPolutsygan Oh OK, if you have the above in a PHP than it would work, Gottcha. I tried looking at the docs but it just didn't click for me. I'm new to this, any help is appreciated.
0

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.

1 Comment

I think this is more of a general question I'm having. The above works (so far so good) but I know I'm not seeing this in the right light and while your answer has just shed some, I am still wondering how programmers handle working with jQuery and PHP, soley with AJAX, or? And if so, could you show me an example based on my code above?

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.