I've a webpage, which is currently pletely 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!
I've a webpage, which is currently pletely 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!
Share Improve this question asked Mar 25, 2011 at 8:33 labrassbanditolabrassbandito 53512 silver badges27 bronze badges 2- Check this question, tons of interesting leads for you : stackoverflow./questions/371875/… – Raveline Commented Mar 25, 2011 at 8:40
- doesn't that talk about local file manipulation, which is a little different from client to server manipulation? – David McLean Commented Mar 25, 2011 at 8:47
7 Answers
Reset to default 2It'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 being -
<script>
alert('Hello')
</script>
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.
Of course you can, check http://phpjs/! ;)
You want to learn how to use AJAX to retrieve data from your server.
it is possible to intergate php to javascript u can just use <?php getFileData()?>
in javascript u can specify var something = <?php getFileData()?>
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, , 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:
- wamp server
- xampp server
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)
As far as I know, you can't work with local files with Javascript. Short answer bees "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://myputer/myfile.html cannot ask http://myserver/givemedates.php for instance)
So considering that you need to use information ing 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 :)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745251238a4618687.html
评论列表(0条)