Adding Duplicate Data Alert Message

Adding Duplicate Data Alert Message

thebumba0712thebumba0712 Posts: 3Questions: 1Answers: 0
edited July 2022 in Free community support

Hello,
I want to know how to make a pop up error message ('duplicate error') after after attempting to add a data with id that is already existing in the datatable. The script I created is already blocking data for being duplicated because I'm using a primary key in sql. I just need an error message to notify that there's already an existing id in the datatable.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Is this using Editor?

    If so, you could validate that the key is unique - please see the example in this section of the manual.

    If not, you would scan the data before adding the row, something like:

        table.rows(function(idx) {
            if (table.row(idx).key === newKey) {
                // show error
            }
        });
    

    Colin

  • thebumba0712thebumba0712 Posts: 3Questions: 1Answers: 0
    edited July 2022
    $(document).on('submit', '#addEmp', function(e) {
      e.preventDefault();
      var empid = $('#addEmpidField').val();
      var lastname = $('#addLastnameField').val();
      var firstname = $('#addFirstnameField').val();
      var middlename = $('#addMiddlenameField').val();
      if (empid != '' && lastname != '' && firstname != '' && middlename != ''){
        $.ajax({
          url: "add_emp.php",
          type: "post",
          data: {
            empid: empid,
            lastname: lastname,
            firstname: firstname,
            middlename: middlename
          },
          success: function(data) {
            var json = JSON.parse(data);
            var status = json.status;
            if (status == 'true') {
              mytable = $('#empList').DataTable();
              mytable.draw();
              alert('success');
              $('#addEmpidField').val('');
              $('#addLastnameField').val('');
              $('#addFirstnameField').val('');
              $('#addMiddlenameField').val('');
              $('#addEmpModal').modal('hide');
    
            } else {
              alert('failed');
            }
          }
        });
      } else {
        alert('Fill all the required fields');
      }
    });
    
  • thebumba0712thebumba0712 Posts: 3Questions: 1Answers: 0

    I'm sorry I have trouble understanding validation, should I validate outside the function or add else if condition and include row count

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    It would be worth looking at Editor - as all these conditions are handled there.

    Colin

Sign In or Register to comment.