I have a system as below: Client-side HTML, JavaScript, and PHP on the server side. The idea is to have a kind of WebService, where an HTML page (it has to be HTML because it will run in an HTML5-based app with Intel XDK) requests information for the server, where PHP returns. >
The problem: When I run this way, the result is the PHP code, as below, ie the server has not processed anything, returns everything written in the PHP file.
<?php
echo $data = date("d/m/Y H:i:s ");
?>
If I load index.html
on the same server where the other files are, then the return is the date, which would be expected, ie only runs if everything is on the same server, but what I need is an HTML file remote, running on any computer that accesses the PHP / JS service on the server.
What's wrong with this setup? Is there a more practical way to do this?
On the local computer ( C:/
), I have the HTML file with the code:
<html>
<head>
<script src="http://code.jquery.c...n.js"></script><scriptsrc="http://www.example.c...S.js"></script>
</head>
<body>
<a href="javascript:test();">Clique aqui...</a>
</body>
</html>
On the server side http://www.example.com
I have a JavaScript and a PHP:
JavaScript file:
function test() {
var jqxhr = $.ajax( "servidor.php" )
.done(function(response) {
alert( "success" );
alert(response);
})
.fail(function(response) {
alert( "error" );
alert(response);
})
.always(function(response) {
alert( "complete" );
alert(response);
});
}
PHP file:
<?php
echo $data = date("d/m/Y H:i:s ");
?>
Update
I've tested in some situations, and now I've located where the problem is that I can not get PHP back. It is in the JS address, which can not reach the PHP file on the server.
This does not work:
$.post('http://www.iodasistemas.meximas.com/JSON/servidor.php',
{comando:true}, function(data){
How it works:
$.post('servidor.php', {comando:true}, function(data){
In short, the whole point is that HTML always calls JS, but it can not reach PHP. Be it running all the files on the server, or a loose HTML on the computer. The error I mentioned occurred because I had a server.php file in the same local HTML folder, so it was the file that it read, not the server. It seems that by loading JS, it starts to work locally, not remote.
How to get JavaScript to arrive in PHP using a complete address http://....
?