0

My php is updating the table but not refreshing in javascript have tried several different ways of doing this and nothing is working. PHP

$sql = "UPDATE INTOXDM.ASTP_FORM SET SUPERVISOR_EID = '".$newSuper."' WHERE FORMID = '".$formId."'";
$row = $xdm->fetch($sql);

$return["color"] = $row['APPRENTICE_SIGNATURE'];

$return["json"] = json_encode($return);
echo json_encode($return);
?>

Javascipt

var data = {
    "formId": formID,
    "newSuper": newSuper
};
data = $.param(data);
$.ajax({
    type: "POST",
    dataType: "json",
    url: "src/GetInfo.php", 
    data: data,
success: function() {
    location.reload();
}

});
CHouse95
  • 111
  • 8
  • 1
    Why are you doing `$return["json"] = json_encode($return);` and then echoing `json_encode($return);`? Why encode the JSON twice? That may be the error, JavaScript may be having trouble decoding the JSON. – gen_Eric Aug 18 '15 at 18:18
  • 1
    You should add an `error: function(jqXHR, status, error){ console.log(status, error); }` function to your `$.ajax` to see what's going on. Also, open your debugger's network tab and see *exactly* what PHP is echoing. – gen_Eric Aug 18 '15 at 18:19

2 Answers2

0

I'd start by modifing the code like this:

var data = {
    "formId": formID,
    "newSuper": newSuper
};

// No need for serialization here, 
// the 'data' parameter of jQuery.ajax accepts JS object
// data = $.param(data);

$.ajax({
    type: "POST",
    dataType: "json", 
    url: "src/GetInfo.php", 
    data: data,

    // As suggested by Rocket Hazmat, try to add an error callback here
    error: function(jQueryXHR, textStatus, errorMessage) {
        console.log("Something went wrong " + errorMessage);
    },

    success: function(jsonResponse) {
        // Try to reference the location object from document/window
        // wd = document or window as seen here http://stackoverflow.com/questions/2624111/preferred-method-to-reload-page-with-javascript
        // Also watch out, usually browsers require a user confirmation before reloading if the page contains POST data

        // One of these should be fine
        wd.location.assign(wd.location.href) : go to the URL
        wd.location.replace(wd.location.href) : go to the URL and replace previous page in history
        wd.location.reload(<true/false/blank>) : reload page from server/cache/cache
    }
});

Also, this might be a shot in the dark but the parameter dataType gave me problems sometime in the past, so if you are sure about the json returned by your php script, you could use the eval function to jsonify the response

$.ajax({
    ...

    // Remove data type
    // dataType: "json", 

    ...

    success: function(plainTextResponse) {
        // Eval response, NOT SAFE! But working
        var jsonResponse = eval('('+ plainTextResponse +')');

        ...
    }
});
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
DevJimbo
  • 98
  • 1
  • 9
0

Your ajax is expecting json data and your php is sending malformed json string. Send a correct json string and your script will work fine.

Your php json_encode should be like this:

$data = json_encode($return);
echo $data;
Crunch Much
  • 1,537
  • 1
  • 11
  • 14