Remote PHP access with Ajax - Return is the PHP code itself

2

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://.... ?

    
asked by anonymous 24.06.2014 / 23:59

3 answers

1

I tried to do this in javascript:

function test(){

$.post('http://www.seusite.com.br/servidor.php', {comando:true}, function(data){

alert(data);

});
}
    
25.06.2014 / 01:04
1

Thank you for your help. I made it work. Below the codes.

I would like someone to give me a light on how to pass a value to the PHP file that is on the server, and how to handle this value in PHP.

So I can make a request to the server and have a processed return.

HTML file + JS (can save anywhere or any computer running)

<script type="text/javascript"> 
var urlTeste = 'http://www.meuservidor.com/servidor.php?jsoncallback=?';
$(document).ready(function() {
//Mensagem enquanto não carrega a pagina
$('#resultado').html('Carregando...');

$.getJSON(urlTeste,null, function(data){
$('#resultado').html(data);   
});
}); 
</script>

PHP file (on server www.myserver.com/servers.php)

<?php
$var = date("d/m/Y H:i:s "); 
echo $_GET["jsoncallback"] . '(' . json_encode($var) . ');';    
?>
    
25.06.2014 / 20:10
0

You can try using jSON to see if you can return what you want.

$.getJSON("www.suapagina.com.br",
              function (data) {

                      alert(data.mensagem);
              }
            )
            .success(function() { alert("Sucesso"); })
            .error(function() { alert("Erro"); })
            .complete(function() {  alert("Completo"); });
    
25.06.2014 / 17:38