1

How can I add validation and php error handling with ajax. Now the success message come correctly but how can I implement error message on it? I might need to add some php validation please help.

Here is my JS.

 $('#edit_user_form').bind('click', function (event) {
  event.preventDefault();// using this page stop being refreshing 
   $.ajax({
    data: $(this).serialize(),
    type: $(this).attr('method'),
    url: $(this).attr('action'), 

    success: function () {
     $(".msg-ok").css("display", "block");
     $(".msg-ok-text").html("Profile Updated Successfully!!");
    },
     error: function() {
     //Error Message          
    }  
  });
}); 

PHP

<?php 
require_once 'db_connect.php';
if($_POST) {
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $index_no = $_POST['index_no'];
    $contact = $_POST['contact'];

    $id = $_POST['id'];

    $sql  = "UPDATE members SET fname = '$fname', lname = '$lname', index_no = '$index_no', contact = '$contact' WHERE id = {$id}";
    if($connect->query($sql) === TRUE) {
        echo "<p>Succcessfully Updated</p>";
    } else {
        echo "Erorr while updating record : ". $connect->error;
    }
    $connect->close();
}
?>
7
  • You are wide open to SQL Injections and should really use Prepared Statements instead of concatenating your queries. Specially since you're not escaping the user inputs at all! Commented Feb 23, 2017 at 6:33
  • You should return a json object instead, with some params like { success: true/false, errorMessage: "some message" } and check it in your success callback for your ajax call. Commented Feb 23, 2017 at 6:34
  • this app is not for web version its an offline web app Commented Feb 23, 2017 at 6:34
  • success: function (data) { $(".msg-ok").css("display", "block"); $(".msg-ok-text").html("Profile Updated Successfully!!"); }, error: function(errordata) { //Error Message } make your success and error block like this, and then through if condition you check for different messages Commented Feb 23, 2017 at 6:37
  • @MagnusEriksson can you show me an example ? Commented Feb 23, 2017 at 6:37

1 Answer 1

1

ajax identifies errors based of status code, your php code will always return status code 200 which is success, even when you get error in php code unless its 500 or 404. So ajax will treat response as success.

if you want to handle php error, make following changes in your code

<?php 
require_once 'db_connect.php';
if($_POST) {
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $index_no = $_POST['index_no'];
    $contact = $_POST['contact'];

    $id = $_POST['id'];

    $sql  = "UPDATE members SET fname = '$fname', lname = '$lname', index_no = '$index_no', contact = '$contact' WHERE id = {$id}";
    if($connect->query($sql) === TRUE) {
        echo "true";
    } else {
        echo "false";
    }
    $connect->close();
}
?>


$('#edit_user_form').bind('click', function (event) {
  event.preventDefault();// using this page stop being refreshing 
   $.ajax({
    data: $(this).serialize(),
    type: $(this).attr('method'),
    url: $(this).attr('action'), 

    success: function (res) {
     if(res == 'true') {
          //success code
     } else if(res == 'false') {
          //error code
     }

    },
     error: function() {
     //Error Message          
    }  
  });
}); 
Sign up to request clarification or add additional context in comments.

2 Comments

Your code works but is it possible to validate form fields
you can use bootstrap validation for the client side

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.