ghipkins
ghipkins

Reputation: 73

Ajax/Jquery communicating with PHP

I am completely new to Ajax (Jquery POST), and I wrote this thing to try to "talk" to a .php file:

function send(d){
    $.post("http://somesite.net/read.php",{data:d})
    .done(function(data){
        document.getElementById('res').innerHTML=data;
    });
}

Read.php:

$d=$_POST["d"];
echo $d;

So, it does return stuff, but it seems like it returns the entire file. It is very likely I am doing something incredibly wrong. I would like to know what it is.

Upvotes: 0

Views: 56

Answers (2)

Janaka R Rajapaksha
Janaka R Rajapaksha

Reputation: 3744

$.post("http://somesite.net/read.php",{data:d})

You have given a full url, but for the security purpose this is NOT ALLOWED

use $.post("./read.php",{data:d}) instead of $.post("http://somesite.net/read.php",{data:d})

Upvotes: 1

kimbarcelona
kimbarcelona

Reputation: 1136

It should be:

$d=$_POST["data"];
echo $d;

See this line:

{data:d}

You are sending variable data with a value d. So in your backend, you should request for variable name.

Upvotes: 2

Related Questions