0

I have a simple checkbox, on click it sends XHR to PHP page , php processes correctly and I use json_encode($response) to return. But instead of a simple true or false I get the source code for the page and it is causing a "parsererror" of course. ajax call as follows

$.ajax({
type: "post",
url: "myprocessor.php",
dataType: 'json',
data: { "id" : idnumber, "action": "makeLive", "isLive" : "1" },
beforeSend: function(data) {
            $("#ajaxInProgress").addClass('progress');
        },
        success: function(data) {
            $("#status").removeClass().addClass((data.error === true) ? 'error' : 'success').text('Success! Appraiser is NO LONGER Live ').slideDown('slow');
        },
        error: function(data) {
            $("#status").removeClass().addClass('error').text(' - Changing the Live status for this appraiser to "Not Live" has failed - APPRAISER IS STILL LIVE IN SYSTEM, please try again').slideDown('slow');
        },

        complete: function(data) {
            $("#ajaxInProgress").removeClass('progress');
            setTimeout(function() {
                $("#status").slideUp('slow').removeClass();
            },2000);
        }
    }); 

The php I post to is as follows:

if (isset($_POST['action'])) { 
if($_POST['action']=='makeLive') { 
    $checkappraiser=mysql_query("SELECT * FROM table WHERE id='".mysql_real_escape_string($_POST['id'])."'");
    if (mysql_numrows($checkappraiser)>0) {
        $livesetting=mysql_result($checkappraiser,0,"live");
        $livesetting=!$livesetting;
        $runSql = mysql_query("UPDATE table SET live='$livesetting' WHERE id='".mysql_real_escape_string($_POST['id'])."'");
        if(!$runSql) {
            $return['error'] = true;
        } else { 
            $return['error'] = false;   
        }
    }
}   
echo json_encode($return);
}

Any suggestions would be great. I am getting the proper data passed I am getting the correct data updated in DB My response is coming back as a parser error because it is trying to parse the source code as a json array.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
Stephen Nielsen
  • 167
  • 1
  • 17

2 Answers2

3

Just a quick check, do you put <?php at the beginning of your php file?

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
1

That, or you're doing something wrong in your webserver, not passing files through to php correctly. Does hitting the php file directly load the source or the result?

If you hit page.php, does it load the same thing as if you hit page.phP or pHP, etc? It matters to web server filters, depending on the web server...

If you use tomcat for java, for example... you can turn off case sensitivity for finding files, but it does not turn off case sensitivity for mapping files to filters or servlets, so .jsp would load the jsp servlet, but .jsP would not.

PersonThing
  • 388
  • 2
  • 10
  • Thanks Tim, but It is not server settings, (I am assuming), because I have other $.ajax calls in this page, all work great, I am using the same syntax as this in those other code blocks as well. But for some reason the response on this one is the source code instead of the json response I am used to. I have googled and checked the other "fixes" that I have found. No Avail ... – Stephen Nielsen Jul 26 '11 at 20:16
  • If you hit the page you're requesting directly in a browser does it work? How about if you hit it via POST like your ajax request is doing? Make a quick form and submit, using the same parameters as your ajax request is passing. Try to mirror the request as exactly as possible, and narrow down the problem. It has to be something about the request or in server settings that the server is deciding to return source code instead of a processed result. The browser has no access to change that, so it's not jQuery's fault that your server is returning source code. – PersonThing Jul 26 '11 at 20:35