javascript - jQuery AJAX return the function as data upon success -
i have codeigniter flashdata + jquery ajax call 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\'s new!', text: res_new, type: 'custom', addclass: 'notification-success', icon: 'fa fa-check' }); } }); }).apply( this, [ jquery ]); </script>
inside $.ajax data, have added both
res_no: res_no, res_new: res_new
which strings text, upon success retrive alert text. want
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 $index => $reservation) { $this->db->insert('reservations', $reservation); } // end foreach return $this->session->set_flashdata('res_new', "success ". count($filtered_reservations) ." reservations inserted!"); print "success ". count($filtered_reservations) ." reservations inserted!"; print "reservations: ". count($kigores_id) ." found on kigo!"; } /* end if */ /** * @filtered_reservations * equal 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 write in data? solution found far window.reload show me notification want refresh..
in order happen, need place 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\'s new!', text: res_new, type: 'custom', addclass: 'notification-success', icon: 'fa fa-check' }); } });
inside ajax success
results, this:
$.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\'s new!', text: res_new, type: 'custom', addclass: 'notification-success', icon: 'fa fa-check' }); } }); } } });
Comments
Post a Comment