Link to home
Start Free TrialLog in
Avatar of Opeyemi AbdulRasheed
Opeyemi AbdulRasheed

asked on

How to Add if statement to Ajax of Bootstrap Notify

Experts!

Is there a way to check type of notify (success or danger) and add a code as in the following code?

I added an automatic button click in the Ajax code so that the DataTable can be refreshed after success. I don't want to perform the button click if there's an error so the DataTable doesn't refresh. Because if it does, the user input will be wiped out.

$('#add_assessment').click(function() {

                $.ajax({
                url     : 'assessment_add_controller.php',
                method  : 'post',
                data    : $('#assessmentForm').serialize(),
                dataType : 'json',
                }).done(function(response) {
                    $('button[name=loadStudents]').click();
                    var type = response.status ? 'success' : 'danger';
                  $.notify({
                    // options
                    message: response.message
                    },{
                    // settings
                      type: type + ' my-notify',
                      placement: {
                        align: 'center' 
                    },
                    animate: {
                        enter: 'animated fadeInDown',
                        exit: 'animated fadeOutUp'
                    }
                                           
                  });

                });

            });

Open in new window


Or can I just perform array_filter() in the Ajax before it gets to Insert/Update in the script?

I'm doing it this way:
$missingCA1 = count( array_filter($_POST['cass1'], function($i) { return $i == 0 || $i > 10; }) );
$missingCA2 = count( array_filter($_POST['cass2'], function($i) { return $i == 0 || $i > 10; }) );
$missingCA3 = count( array_filter($_POST['cass3'], function($i) { return $i == 0 || $i > 10; }) );
$missingExam = count( array_filter($_POST['exam'], function($i) { return $i == 0 || $i > 70; }) );

    if ( $missingCA1 ){
        $response->message = "CA1 scores must be greater than <strong>0</strong> and NOT more than <strong>10</strong>";
        die( json_encode($response) );
    }
    if ( $missingCA2 ){
        $response->message = "CA2 scores must be greater than <strong>0</strong> and NOT more than <strong>10</strong>";
        die( json_encode($response) );
    }
    if ( $missingCA3 ){
        $response->message = "CA3 scores must be greater than <strong>0</strong> and NOT more than <strong>10</strong>";
        die( json_encode($response) );
    }
    if ( $missingExam ){
        $response->message = "Exam scores must be greater than <strong>0</strong> and NOT more than <strong>70</strong>";
        die( json_encode($response) );
    }

Open in new window


Thank you
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Opeyemi AbdulRasheed
Opeyemi AbdulRasheed

ASKER

Hehehe! O ga o. That was awesome. Thanks sir.