labrassbandito
labrassbandito

Reputation: 535

Can one integrate PHP in Javascript?

I've a webpage, which is currently completely build with PHP. Now, it would be great if it works on my local machine outside the web server, too. I figured out, that most of the stuff is just client side operations. I can easily rewrite them using Javascript. However, at one point, I am reading Date information from files stored on the server as follows (PHP):

function getFileDate($file) {
  $time = filemtime($file);
  $date = date('d.m.y', $time);
  return $date;
}

Is it possible to integrate that call into Javascript? And, can I implement some kind of switch, so that it will only be executed on my local machine (maybe replaced by a dummy date)?

Thanks for your help!

Upvotes: 0

Views: 7184

Answers (7)

tamarintech
tamarintech

Reputation: 1982

It's important to know that Javascript is executed by the client, PHP is executed by whatever is serving your page.

With that being said, you can easily use PHP to "echo" or otherwise "inject" values into the middle of blocks in your HTML page.

A "quick and dirty" example -

<script>
alert('<?php echo Hello ?>')
</script>

Results in the final page source becoming -

<script>
alert('Hello')
</script>

Upvotes: 2

powtac
powtac

Reputation: 41050

Of course you can, check http://phpjs.org/! ;)

Upvotes: 1

Rodolphe
Rodolphe

Reputation: 497

As far as I know, you can't work with local files with Javascript. Short answer becomes "you can't change your script to work without a webserver behind."

For the kind of feature you're building, you may use some ajax queries though : you ask some php pages on your server (php or any other language) to retrieve only the piece of data you need.

Ajax calls are working only on the same webserver. You can't access file from another domain. (http://mycomputer/myfile.html cannot ask http://myserver/givemedates.php for instance)

So considering that you need to use information coming from files, we stated that you need to have a serverside scripting language (php). My conclusion : I think your best option is not to rewrite your whole page, but to deploy a server :)

Upvotes: 0

David McLean
David McLean

Reputation: 1497

As a very basic answer to your question, no.

You can not read information of files that sit on a server with javascript, javascript is client side and client side only, unless you make use of ajax. Ajax is simply javascript calling a server page (could be php, .net, java etc) and then working with the response that is returned to it.

If you want to run your script locally it might be better to setup a local php server.

A couple of nice easy to use servers include:

I personally use wamp server.

You can not achieve everything in javascript that you can in php, for example saving information to your database. you could do an ajax call to save data but that would be using some server side code somewhere.. and then you wouldn't be able to test that on your local machine.

If you have a local server (as you should as part of your development process) then you will be able to do debugging of your php code easily with tools like netbeans IDE and Xdebug. (How to set up netbeans and xdebug)

Upvotes: 0

Siva
Siva

Reputation: 3728

it is possible to intergate php to javascript u can just use <?php getFileData()?> in javascript u can specify var something = <?php getFileData()?>

Upvotes: 0

CoolStraw
CoolStraw

Reputation: 5390

It doesn't make sense. Javascript is client-side, php is server side. If you have a file on the server that has to be read, then PHP. You might want to add a ajax call to execute that portion of php code.

That's how it's supposed to work, when your javascript needs data it queries a php file on the server.

Upvotes: 1

Paul
Paul

Reputation: 6871

You want to learn how to use AJAX to retrieve data from your server.

Upvotes: 0

Related Questions