Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

jQuery .post syntax



5:12 pm on Jun 25, 2012 (gmt 0)

10+ Year Member

I'm using jQuery .post() [see: [api.jquery.com ]] to send data to a PHP file for processing and can't get it to work. It's likely a simple syntax error, but I don't see it. Here's my code:

function do_php(){
var $data = "test";
$.post("/file.php", {data:$data});
window.onload = do_php;

My PHP file reads the data via:

$my_var = $_POST['data'];

I had this working at one point, but something got changed somewhere along the line & I can't get back.

If it helps, I don't even get info when I add the 'success' callback function:

$.post("/file.php", {data:$data}, function(data){alert(data);});


7:16 pm on Jun 25, 2012 (gmt 0)

10+ Year Member

RESOLVED - this was a path issue; .post() doesn't accept relative paths except in the same directory.


1:20 pm on Jun 26, 2012 (gmt 0)

10+ Year Member

In case anyone is following this or finds it via search, here's a critical update:

jQuery .post() DOES accept relative paths; what happened in my case is that, for general website security, I added an .htaccess file with "deny from all" to a directory that contained my php script. Since jQuery .post() requests the page via HTTP, my .htaccess denied it.

So if you're sure your code is right but get no result, that may be why.

Live & learn.

Featured Threads

Hot Threads This Week

Hot Threads This Month