javascript - AJAX giving success but no changes made in database

835

I am making a function to change the user's email, this call will be made via AJAX on a button click. The form code is below, which is in a file namedprofile.php:

<form id="changeEmail" method="post">
  <div >
    <label for="changeEmail">Update Email Address</label>
    <input type="email" id="email" name="email" value="<?php echo $currentEmail; ?>">
  </div>
  <button type="submit" id="updateEmail" >Update</button>
</form>

I have also created ascript in this file as well to perform some basic functionality such as call the ajax function and hide the button until changed. See it below:

<script>
     $("#updateEmail").hide();
     var id = "<?php echo $id ?>";

     $("#email").change(function(){
        $("#updateEmail").slideDown();
     });

     var email = $("#email").val();

     console.log("id: " + id);
     updateEmail("<?php echo $id; ?>", email);
</script>

The AJAX function which I created is saved in a file namedprofileAjax.js, this file will hold all my profile ajax functions passing through the users id and new email address.

function updateEmail(id, email) {
$("#updateEmail").click(function(e) {
    e.preventDefault(); // Prevent HREF

    $("#spinner").show(); // Show spinner

    setTimeout(function() {
        $.ajax({ // Perform Ajax function
            url: "../ajax/admin/updateEmail.php",
            dataType: "HTML",
            type: "POST",
            data: {id: id, email: email},
            success: function (result) {
                $("#spinner").hide();
                $(".dashContent").html(result);
                console.log("This worked");
            }
        });
    }, 1500); // Delay this for 1.5secs
}); 
}

The final file which is called by the AJAX function isupdateEmail.php which can be seen below:

include '../../functions/linkAll.inc.php';

$id = filter_input(INPUT_POST, "id");
$email = filter_input(INPUT_POST, "email");
updateEmail($id, $email);

The function which is calledupdateEmail is saved in an external file and works fully on its own.

function updateEmail($id, $email) {
$connect = db();

$stmt = $connect->prepare("UPDATE `Account` SET `email` = ? WHERE `id` = ?");
$stmt->bind_param("si", $email, $id);

if ($stmt->execute()) {
    successMessage("Successfully updated your email address.");
} else {
    errorMessage($stmt->error());
}

$stmt->close();
}

However, when the update button is shown and clicked upon, it runs and gives a success message with nothing being updated in the database table itself.

814

Answer

Solution:

The jQuery AJAX success function is called when the requested source returns HTTP status 200. You should return something from PHP to the AJAX call to tell it if the database action was successfull.

Example PHP (called by AJAX request):

if($stmt->execute()) {
  echo "1";
}
else {
  echo "0";
}

Example JavaScript:

success: function(data) {
  if(data == "1") {
    //code if database action is successfull
  }
  else {
    //code if database action failed
  }
}

People are also looking for solutions to the problem: Do we need to have persistant object to achieve singleton pattern in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.