-1

Issue seems to be that it's firing the error handler, never the success..

$('#editPlaceForm').submit(function() {
        var placeName = $('#placeName').val();
        $.ajax({
            type: 'POST',
            url: '../FormHandlers/myPlaces.php',
            dataType: 'json',
            data: {update_Place:true, place_Id : placeId, place_Name : placeName },
            success:function(json) {
                var result = JSON.parse(json);
                if (result.success) {
                    $('#editPlaceModal').modal('hide');
                    if (locationsTable) {
                        locationsTable.fnDraw();
                    }
                }
            }, error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('Sorry, an error occurred: Thrown: ' + errorThrown + ', Request: ' + XMLHttpRequest.getAllResponseHeaders().toString() +
                ', TextStatus: ' + textStatus + ', Please try again.');
            }
        });
    });

Server-side being called via ajax:

if (isset($_POST['update_Place'])) {

    $place_id = htmlspecialchars($_POST['place_Id']);
    $place_name = htmlspecialchars($_POST['place_Name']);

    $update = "UPDATE locations SET name='" . $place_name . "' WHERE id =" . $place_id . ";";

    $db->query($update);
    $db->commit();
    $db->close();

    echo json_encode(array('success'=>'Changes were saved.'));
}

Many thanks..................................

10
  • 4
    And what error are you getting? Try console.log() instead of alert() so that you can more easily copy and paste the error into your question. (Note that you don't need to use JSON.parse() within your success handler, jQuery will parse it for you before calling the function because you specified dataType: 'json'. This isn't causing your current problem, but once you solve the current problem it would be your next problem.) Commented Jun 30, 2013 at 0:02
  • 3
    I believe that this data: 'updatePlace=' + JSON.stringify(data) is incorrect since you do a post type not get you should try this instead data: {updatePlace: JSON.stringify(data)} Commented Jun 30, 2013 at 0:03
  • hmm, I tried data: { updatePlace: ... } seems to be giving me the same result. There's nothing showing up for Thrown or Request, and TextStatus: just says 'error' Commented Jun 30, 2013 at 0:07
  • @mapleafman then most likely the problem is even before that, just like a Lex noted in his answer. Commented Jun 30, 2013 at 0:08
  • 1
    Seems like the errorThrown is blank, but for future reference don't do a + string concatenation of XMLHttpReqest - log each argument separately. Commented Jun 30, 2013 at 4:30

2 Answers 2

2

The first thing that jumps at me is this:

url: '../FormHandlers/myPlaces.php',

I am not sure where and how the '..' is handled, but you can write to a log from your php to check if the script file is even being run.

Sign up to request clarification or add additional context in comments.

Comments

0

Okay, so I added 'return false;' after the ajax block, and did one or two other things: apparently this code works fine:

front end:

$('#editPlaceForm').submit(function() {
        var placeName = $('#placeName').val();
        $.ajax({
            type: 'POST',
            url: "../FormHandlers/myPlaces.php",
            dataType: 'json',
            data: {update_Place:true, place_Id : placeId, place_Name : placeName},
            success: function(json) {
                alert(json.success);
                if (json.success) {
                    $('#editPlaceModal').modal('hide');
                    if (locationsTable) {
                        locationsTable.fnDraw();
                    }
                }
            }, error: function(XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest + ', ' + textStatus + ', ' + errorThrown);
                alert('Sorry, an error occurred: Thrown: ' + errorThrown + ', Request: ' + XMLHttpRequest.getAllResponseHeaders().toString() +
                ', TextStatus: ' + textStatus + ', Please try again.');
            }
        });
        return false;
    });

server:

if (isset($_POST['update_Place'])) {

$place_id = htmlspecialchars($_POST['place_Id']);
$place_name = htmlspecialchars($_POST['place_Name']);

$update = "UPDATE locations SET name='" . $place_name . "' WHERE id =" . $place_id . ";";

$db->query($update);
$db->commit();
$db->close();

echo json_encode(array('success'=>'Changes were saved.'));
}

1 Comment

To not submit the form you could change the following: $('#editPlaceForm').submit(function(e) { and the first line in the function should be: e.preventDefault()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.