0

I am stumped on this issue. I am trying to simply post one variable via ajax. Here is what I have for my javascript/jquery code...

var name = "Bob";

$(".save").click(function(){

         $.ajax({
             type: "POST",
             url: "ajax.php",
             data: {fname:name}
             }).done(function( msg ) {
             alert( "Data: " + msg );
         });

 });

And here is my ajax.php file:

<?php
$data = $_POST['fname'];
?>

The problem is I always get this error in my php error log...

[15-Aug-2013 11:05:26] PHP Notice: Undefined index: fname in /Applications/MAMP/htdocs/Project/ajax.php on line 2

What am I doing wrong here?

Dustin
  • 4,314
  • 12
  • 53
  • 91
  • Have you try `var_dump` your `$_POST`? – Hieu Le Aug 15 '13 at 15:14
  • Use something like Firebug, and echo your $_POST. with Firebug you can see the response you're getting. But I agree with Trung-Hieu. It sounds like your POST isn't being set correctly. – JRizz Aug 15 '13 at 15:15
  • With Firebug I can see the status on ajax.php is '302 Found' and if I click the Post tab I can see the correct data in there. Still getting that undefined index error though. Weird. – Dustin Aug 15 '13 at 15:22
  • Try wrapping it in a if(isset($_POST['fname'])) and see if that fixes it – JRizz Aug 15 '13 at 15:24
  • Did you try Trung's suggestion? Instead of `$_POST['fname']`, send back a `var_dump` of `$_POST`. – Asad Saeeduddin Aug 15 '13 at 15:32
  • 1
    _“the status on ajax.php is '302 Found'”_ – so a redirect. Is this really what you intended? Do you yourself make that redirect from your PHP script, _after_ it processed the POST data? (POST data will get lost on 302 redirects, because the browser will request the new URL via GET.) – CBroe Aug 15 '13 at 15:34
  • Hmmm nope, not intending to do a redirect. My ajax.php file literally has that one line of code that I posted in my question. – Dustin Aug 15 '13 at 15:36
  • @Asad would I just do this in my ajax.php file? `var_dump($_POST['fname']);` Not sure where/how to see what that returns to me though. – Dustin Aug 15 '13 at 15:41
  • @DustinMcGrew Yes, that's correct. It should show up in `#msg`. – Asad Saeeduddin Aug 15 '13 at 15:43
  • Ok I did that.. it's not returning anything :( – Dustin Aug 15 '13 at 15:46
  • @DustinMcGrew Hold on, I misread your code a bit. I thought you were echoing the data into `#msg`. Do you at least see the text "ajax done" in that element? – Asad Saeeduddin Aug 15 '13 at 15:56
  • @Asad Yes I see that. I actually just changed my code to echo the data into #msg. I'll update the code in my question to what I have now. – Dustin Aug 15 '13 at 15:59
  • 1
    Read the 302 response, check the new location, and use it on ur ajax call, it could be that it's redirecting to the same url but different format, like removing www, or adding a slash, check that – Mohammad AbuShady Aug 15 '13 at 16:11
  • Thanks @MohammadAbuShady!! I have a .htaccess file that allows .php pages to work without having .php in them. Something with that was screwing up the ajax.php page. – Dustin Aug 15 '13 at 16:24

1 Answers1

0

Your code is absolutely fine. Maybe your problem came from .htaccess and it made a redirection when you access the php file. You should double check it. Deny ajax file access using htaccess

Community
  • 1
  • 1
Telvin Nguyen
  • 3,569
  • 4
  • 25
  • 39