homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

jQuery .post syntax

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

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)

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)

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved