简体   繁体   中英

CodeIgniter 3: Submit to Controller in Ajax

I'm trying to create a notification feature and I wanted the ajax to be able to run a query via the controller on button click

THIS IS MY SCRIPT

 $('#noti_Button').click(function (e) {
            e.preventDefault();
             $.ajax({
                url: '<?php echo site_url("profile/read_notif")?>'

            });
});

THE CONTROLLER

public function read_notif(){
        $this->profile_model->read_notifs($data['id']);
        return;
    }

AND THE MODEL

 function read_notifs($id)
    {
        $read = array(
        'read' => '1'
        );
        $this->db->where('recipient', $id);
        $this->db->update('tbl_notifications', $read);
        return;
    }

I tried this and the data in the database doesn't update. IN MY HTML IT IS JUST A SIMPLE BUTTON

It is the sample calling the ajax in ruby on rails . In this code we are calling the controller for getting the values.

$('#Button').on('change', function(event) {
  var selected_resource_id = $(this).val();
  $.ajax({ 
   type: 'GET', 
   url: "<%= Home_index_path %>",
   data: { id: selected_resource_id }, 
   success: function (data) {  
    alert("Ajax success");
   }
  });
});

Script

$('#noti_Button').click(function (e) {
        e.preventDefault();
         $.ajax({
            url: '<?php echo site_url("profile/read_notif")?>',
            success:function(data)
            {
                alert(data);
            }

        });
    });

Controller

public function read_notif(){
        $this->profile_model->read_notifs($data['id']);
        echo $this->db->last_query();
    }

use error: to check if there's any error in submitting your form.

$('#button').click(function (e) {
        e.preventDefault();
         $.ajax({
            url: '<?php echo site_url("profile/read_notif")?>',
            success:function(data)
            {
                alert(data);
            }

        });
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM