JQuery AJAX return function as data on success

I have a codeigniter flashdata + jQuery AJAX call to show it. code:

<script type="application/javascript">

var res_no = '<?php echo $this->session->flashdata('res_no'); ?>';
var res_new = '<?php echo $this->session->flashdata('res_new'); ?>';

(function( $ ) {

$("#check-reservations").click(function() {

   $.ajax({
            type: "POST",
            url: "mycontroller/function",
            async: true,
            data: {
            res_no: res_no,
            res_new: res_new
            },
            success: function(data) {   
            if(data) {
            alert(data);
            }
           }
        });
     });

/*
Default Notifications
*/
$('#check-reservations').show(function() {

    if (res_no) {
    new PNotify({
        title: 'Hmm no new Reservations..',
        text: res_no,
        type: 'custom',
        addclass: 'notification-primary',
        icon: 'fa fa-info-circle '
    });
    }

    else if (res_new) {
    new PNotify({
        title: 'There\ something new!',
        text: res_new,
        type: 'custom',
        addclass: 'notification-success',
        icon: 'fa fa-check'
    });
    }
   });
   }).apply( this, [ jQuery ]);
 </script>

      

inside the $ .ajax data, I added both

res_no: res_no,
res_new: res_new

      

which are just lines of text, upon success I return back a warning with text. I want to come back

new PNotify({
        title: 'Hmm no new Reservations..',
        text: res_no,
        type: 'custom',
        addclass: 'notification-primary',
        icon: 'fa fa-info-circle '
    });

      

PHP:

 /**
    * @filtered_reservations
    * @index
    */
    $filtered_reservations = $this->filter_array($reservations);          
    if (count($filtered_reservations) > 0) {
    foreach ($filtered_reservations as $index => $reservation) {
    $this->db->insert('reservations', $reservation);   
    } // end foreach   
    return $this->session->set_flashdata('res_new', "Success ". count($filtered_reservations) ." Reservations were Inserted!"); 
    print "Success ". count($filtered_reservations) ." Reservations were Inserted!";
    print "Reservations: ". count($kigores_id) ." found on kigo!";
    } /* end if */


    /**
    * @filtered_reservations
    * equal to 0
    */
    elseif (count($filtered_reservations) === 0) {
    print "Sorry no new Reservations!";
    return $this->session->set_flashdata('res_no', 'Sorry no new Reservations!');
    //$this->ci_alerts->set('warning', "Sorry no new Reservations!");
    }

    } /* end reservations */     

      

What should I write in the data? The only solution I have found so far is window.reload which will show me a notification the way I want but with an update.

+3


source to share


1 answer


For this to happen, you need to put this:

$('#check-reservations').show(function() {
    if (res_no) {
        new PNotify({
            title: 'Hmm no new Reservations..',
            text: res_no,
            type: 'custom',
            addclass: 'notification-primary',
            icon: 'fa fa-info-circle '
        });
    } else if (res_new) {
        new PNotify({
            title: 'There\ something new!',
            text: res_new,
            type: 'custom',
            addclass: 'notification-success',
            icon: 'fa fa-check'
        });
    }
});

      



inside your AJAX results success

like:

$.ajax({
    type: "POST",
    url: "mycontroller/function",
    async: true,
    data: {
        res_no: res_no,
        res_new: res_new
    },
    success: function(data) {   
        if(data) {
            $('#check-reservations').show(function() {
                if (res_no) {
                    new PNotify({
                        title: 'Hmm no new Reservations..',
                        text: res_no,
                        type: 'custom',
                        addclass: 'notification-primary',
                        icon: 'fa fa-info-circle '
                    });
                } else if (res_new) {
                    new PNotify({
                        title: 'There\ something new!',
                        text: res_new,
                        type: 'custom',
                        addclass: 'notification-success',
                        icon: 'fa fa-check'
                    });     
                }
            });
        }
    }
});

      

+1


source







All Articles