Does javascript work in a php file?

I was simply wondering if javascript could work in a php file.

 <html>
 <head></head>
 <body>
  <script type="text/javascript" src="js/main.js"></script>
 </body>
</html>

I tried a few Alert(); lines and they work great but when i try to add some event listeners to a “a tag”. Nothing seems to work for me. The javascript is simple, just a matter of turning a box from display: block; to none;

function toggle(){
 document.getElementById("blueBox").style.display === none;
 if( document.getElementById("blueBox").style.display === none){
     document.getElementById("blueBox").style.display === block;
  }else{ document.getElementById("blueBox").style.display === none;}
 }

Just a wondering, if javascript doesnt work in php, how can i include javascript in a php based website.

If Javascript doesn’t work with PHP, then Facebook wouldn’t exist. The reason why your Javascript isn’t working is because the Javascript itself has errors. That and you don’t have any actual Javascript libraries you are using.

Yeah that wouldnt make tons of sense lol. I appreciate the clarification! I think i need to keep working on the javascript code in itself. I will try a bit longer but once i feel like i cant do it anyone, i will post it here.

By library what do you mean? What library would i need? If im using simple RAW javascript.
PS: havent done javascript in a few months so i cant remember it much!

Sometimes, Javascript codes won’t just work alone. You need a library to help it work.

Try embedding this Javascript file into your PHP document. Place it before your main.js file.

https://code.jquery.com/jquery.min.js

It might help and it might not. It all depends on what that function requires.

Wait really? Javascript wont work in php without a JQUERY library? I have no knowledge of jQuery and don’t find the need of learning it at the time. There has to be another way for javascript to work smoothly in a php file.

That’s not what I meant and it’s not just in a PHP document. With any document, it might not work because of the functionality and requirements. I don’t think you understand what I’m talking about. Sometimes the functions require more than just the Javascript itself and sometimes it works alone.

What you need to understand is that it’s not just with PHP. If you create the same Javascript snippet in say file.html, you will still get the exact same results. It has nothing to do with whether you save it in file.php or file.html.

For a browser, there are no files on a web server.

But HTTP resources only. And resource type is identified by the Content-type HTTP header.

Both html and php files by default are served with text/html content type and thus to a browser they are all the same.

It has nothing to do with PHP.
PHP can produce code in whatever text based language, it could be html, css, xml or javascript. What the browser sees is just the end product of the php, it’s oblivious to whether the code was generated from php or was hand written.
As for jquery, if you are using jquery functions, you need jquery. If you just use vanilla js, you don’t need jquery.
If your js does not work, it is because of errors, not because of php.

1 Like

Unless you’re explicitly using functionality of another library, it will. ;-) While many people seem to include random libraries such as jQuery “just in case” when they eventually only need it for, say, a few display toggles, I’d usually advocate the contrary approach and only start pulling additional code when you really need it as to not unnecessarily bloat your project. At any rate, I don’t see what this has to do with the OP.

@JonathanVazquez, there are a few issues with your above code, but this is can certainly be achieved w/ vanilla JS. The two major things are that you’re mixing up === assertions and = assignments, and that you should put quotes around none and block as these expressions are otherwise treated as variables.

1 Like

Hi there JonathanVazquez,

you can see a working example of javascript within a php file here…

http://coothead.co.uk/currency-converter/

This is it’s HTML…

<script src="scripts/converter.php"></script>

…and this is the “converter.php” file…

<?php
$xml=simplexml_load_file('http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in (
"EURAED","EURARS","EURAUD","EURAWG","EURBAM","EURBBD","EURBDT","EURBGN","EURBHD","EURBMD","EURBOB",
"EURBRL","EURBSD","EURCAD","EURCHF","EURCLP","EURCNY","EURCOP","EURCYP","EURCZK","EURDKK","EURDOP",
"EUREGP","EUREUR","EURFJD","EURGBP","EURGHS","EURGMD","EURGTQ","EURHKD","EURHRK","EURHUF","EURIDR",
"EURILS","EURINR","EURIRR","EURISK","EURJMD","EURJOD","EURJPY","EURKES","EURKHR","EURKRW","EURKWD",
"EURLAK","EURLBP","EURLKR","EURLTL","EURLVL","EURMAD","EURMDL","EURMGA","EURMKD","EURMUR","EURMVR",
"EURMXN","EURMYR","EURNAD","EURNGN","EURNOK","EURNPR","EURNZD","EUROMR","EURPAB","EURPEN","EURPHP",
"EURPKR","EURPLN","EURPYG","EURQAR","EURRON","EURRSD","EURRUB","EURSAR","EURSCR","EURSEK","EURSGD",
"EURSYP","EURTHB","EURTND","EURTRY","EURTWD","EURUAH","EURUGX","EURUSD","EURUYU","EURVEF","EURVND",
"EURXAF","EURXCD","EURXOF","EURXPF","EURZAR")&env=store://datatables.org/alltableswithkeys');

$r=[];

foreach($xml->results->rate as $node) {
foreach($node->Rate as $node0) {
  array_push($r,$node0);
 }
}
foreach($xml->results->rate->Date as $node) {
  $date="'$node'";
 }
foreach($xml->results->rate->Time as $node) {
  $time="'$node'";
 }

$end=count($r)-1;

?>
(function() {
   'use strict';

   var rates=[<?php for($x=0;$x<$end;$x++) {echo "$r[$x],";} echo "$r[$end]"; ?> ];
   var dte=<?php echo "$date"; ?>;
   var tme=<?php echo "$time"; ?>;

   var cur0,cur1,swop,amount0,amount1,date,m,d,y,time,test=true;
   var mth=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];

if( document.getElementById('converter')) {
   document.getElementById('theform').reset();

   cur0=document.getElementById('cur0');
   cur1=document.getElementById('cur1');

   amount0=document.getElementById('amount0');
   amount1=document.getElementById('amount1');

   swop=document.getElementById('swop');
   date=document.getElementById('thedate');
   time=document.getElementById('thetime');

cur0.onchange=function() {
if(test===true) {
   amount1.value=amount0.value;
   amount0.value=(rates[cur0.value]*amount1.value/rates[cur1.value]).toFixed(2);
   if(amount0.value===0) {
      amount0.value='';
    }
 }
else {
   amount1.value=(rates[cur1.value]*amount0.value/rates[cur0.value]).toFixed(2);
   if(amount1.value===0) {
      amount1.value='';
    }
  }
 }
cur1.onchange=function() {
if(test===true) {
   amount1.value=amount0.value;
   amount0.value=(rates[cur0.value]*amount1.value/rates[cur1.value]).toFixed(2);
   if(amount0.value===0) {
      amount0.value='';
    }
 }
else {
   amount1.value=(rates[cur1.value]*amount0.value/rates[cur0.value]).toFixed(2);
   if(amount1.value===0) {
      amount1.value='';
    }
  }
 }
   amount0.onkeyup=function() {
   amount0.value=this.value;
   amount1.value=(rates[cur1.value]*amount0.value/rates[cur0.value]).toFixed(2);
   if(amount1.value===0) {
      amount1.value='';
    }
 }
   amount1.onkeyup=function() {
   amount1.value=this.value;
   amount0.value=(rates[cur0.value]*amount1.value/rates[cur1.value]).toFixed(2);
   if(amount0.value===0) {
      amount0.value='';
    }
 }
swop.onclick=function() {
if(test==true) {
   amount1.value=amount0.value;
   amount0.value=(rates[cur0.value]*amount1.value/rates[cur1.value]).toFixed(2);
   if(amount0.value===0) {
      amount0.value='';
    }
   swop.innerHTML='▲';
   test=false;
 }
else {
   amount0.value=amount1.value;
   amount1.value=(rates[cur1.value]*amount0.value/rates[cur0.value]).toFixed(2);
   if(amount1.value===0) {
      amount1.value='';
    }
   swop.innerHTML='▼';
   test=true;
  }
 }                                                  
   m=dte.split('/')[0]-1;
   d=dte.split('/')[1];
   y=dte.split('/')[2];
   date.innerHTML=mth[m]+' '+d+', '+y;
   time.innerHTML=tme;
 }
})();

coothead

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.