JavaScript and PHP; Using a JavaScript Include to Run PHP

JavaScript and PHP do not play well together. JavaScript is client side, PHP server side. This makes it difficult to get things to play together. One way to get them to play together, is to have your PHP script build your JavaScript. Now, what if you need to have an external JavaScript file get some stuff from your database and run some things in PHP? Something like this widget:

This is a widget that I wrote. It pulls a list from a database and then displays it. I want people on other websites to be able to display it, so I need a way for them to include it on their site. To do that, I use the <script> tag:


<script type="text/javascript" src="http://contestfarm.com/scripts/cf/rand_contest.php?style=2&uid=1"></script>

I want you to notice a couple of things that are different about this than normal JavaScript includes. First, notice that the file extension is .php, not .js. If I called it .js, then it would not run as a PHP file, and I need it to run as PHP. Second, notice that I included two variables. This allows us to pass variables, just like you would in any PHP script.

OK. Not that we have the hosting site set to call it, how do we make it display something? Well, if I was doing it in HTML/PHP it would have code something like:


echo "<a href='" . $url . "'>" . $title . "</a>";

This is all fine and dandy, if I am calling it as a PHP script, but I'm calling it as a JavaScript. Since it is JavaScript, it does not know the HTML tags as commands, so the script fails. We need to make it JavaScript, so we have to add in a document.write. Now, this line gets longer and more confusing. We have to remember to close the HTML tag, we have to close the JavaScript command, and we have to close the PHP command. This is what we end up with:


echo "document.write(\"<a href='" . $url . "'>" . $title . "</a>)\";";

In PHP, you can stop your PHP script and display some HTML. ie:


?>
<a href="http://shabamdevelopment.com">Shabam Development Rocks
//more php here

We can do something like this, but remember, that we are doing JavaScript, so we can't bail out, use HTML and then go back. We can bail out of the PHP, but not the JavaScript. We still need the document.write. This will work:


?>
document.write('<a href="http://shabamdevelopment.com">Shabam Development Rocks');
//more php here

One last thing. We have to set the header of our file to be JavaScript. At the top of the page include a header, like this:


<? Header('Content-Type: application/x-javascript')?>

So now lets put it all together.

In the hosting page:


<script type="text/javascript" src="http://contestfarm.com/scripts/cf/rand_contest.php?style=2&uid=1"></script>

In the script:

<? Header('Content-Type: application/x-javascript')?>
document.write('<a href="http://shabamdevelopment.com">Shabam Development Rocks');
//more php here

Comments

Pingback

[...] Isbell presents JavaScript and PHP; Using a JavaScript Include to Run PHP posted at Shabam Development, saying, “Server side and client side programs often don’t [...]

Pingback

[...] Isbell presents JavaScript and PHP; Using a JavaScript Include to Run PHP posted at Shabam Development, saying, “Learn how to get your client side JavaScript and [...]

Blog Carnival

Congradulations! This post has been picked to feature in the Spotlight on SpotlightBlogger.com!!!
Check it out at:
SpotlightBlogger.com

Pingback

[...] 5 . JavaScript and PHP; Using a JavaScript Include to Run PHP [...]

Thank you

This is a great tutorial, thank you, you've made it very easy to understand.

Does this mean that you don't have to have a .js script?

No, you don't need a js

No, you don't need a js script for this to work. This method uses just the php script with the javascript embedded in it. If you have a specific problem you would like me to look at feel free to post it here. If I can, I will write up instructions to answer the questions and post them on the site.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h1> <h2> <h3> <h4> <h5> <h6>
  • Lines and paragraphs break automatically.
  • Use the special tag [adsense:format:slot] or [adsense:format:[group]:[channel][:slot]] or [adsense:block:location] to display Google AdSense ads.
  • You may use [view:viewname] tags to display listings of nodes.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.