Jump to content

adding php in javascript?


xwishmasterx

Recommended Posts

I have part of code as shown below:

  newdiv.innerHTML = "<a href=\"javascript:;\" onclick=\"removeElement(\'"+divIdName+"\')\"><div style='position:relative;top:5px;left:980px'><img src='luk.png' style='position:absolute' height='50' width='50'></div></a><div align='center'>THIS IS SHOWNG FINE</div>

Basically clicking a link will show "THIS IS SHOWING FINE".

 

I want it include som php instead like <?php include "myfile.php"; ?>

 

How  can this be achieved?

Link to comment
Share on other sites

JavaScript and PHP do completely different things.

PHP works as hypertext preprocessor (hence its name, "PHP: Hypertext Preprocessor") and does not generate any events in the browser, it just helps to dynamically display content by querying databases or by checking unique identifiers given.

JavaScript is client-side and gives you options to modify the document content, send asynchronous HTTP requests and generally does not provide dynamic content (unless you send, as mentioned before, HTTP requests).

 

If the file you want to include contains pieces of JavaScript and you set the HTTP Content-Type to "application/javascript" (or for older browsers, "text/javascript"), you can do this.

<script src="myfile.php" type="text/javascript"></script>

 

Otherwise, you can echo content into the script tag itself.

<script type="text/javascript"><?= $yourVariable ?></script>

Link to comment
Share on other sites

What I do not understand is basically the script just ouputs html code, why can't it simply output php?

Because PHP code is parsed on the server when the .php file is requested. Browsers can only parse client side code such as html, javascript and css.

Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.