简体   繁体   中英

codeigniter sending a variable from ajax to controller

I'm currently doing an ajax add,update and delete. And I think I'll just start with the delete since it is the easiest and hope that it might help me in the others.

In jquery (this is inside $doc.ready and the event is triggered properly)

if ($a == "Delete") 
{
    var postid = $(this).next('.postid').val();
    $(this).closest(".todo-content").fadeOut();
    jQuery.ajax({
        type: "POST",
        dataType: 'json',
        url: "<?=base_url()?>.index.php/classes/deletepost",
        data: {postid: postid},         
        async: false,
  });
}

in html

<form method="post">
    <button class="btn" onclick="return confirm('Are you sure to delete this item?')">Delete</button>
    <input type="hidden" value="<?php echo $id; ?>" name="postid">
</form>

In controller

public function deletepost(){
        $id = $this->input->post('postid');
        $data = array('active' => 0);
        $this->Model_name->deletepost($id,$data);
        redirect('/abc/123');
    }

This is already working but then I am planning on making the crud to ajax. I'm trying to pass the postid from ajax to controller to delete this post. The fadeout already works but only the ajax does not. I'm very new to ajax so I do not know where I am going wrong and I might also ask questions again regarding the other parts of crud.

Fixed!

The problem was the url inside the $.ajax . It returns a garbage.

So I added a script in the header

<script type="text/javascript">
    var BASE_URL = "<?php echo base_url();?>";
</script>

And just use BASE_URL in the url: like so url: BASE_URL+'classes/deletepost',

Please Try to follow this:

In Codeigniters View:

<!-- Store ID and baseurl as attributes . this would help you to fetch data -->
    <button class="btn" postId="5" baseUrl="<?php echo base_url();?>" id="button">Delete</button>


    <!-- Store ID and baseurl as attributes . this would help you to fetch data -->
    <button class="btn" postId="5" baseUrl="<?php echo base_url();?>" id="button">Delete</button>
    <!-- reading jquery file .. -->
    <script type="text/javascript" src="http://localhost/jquery/js_search/jquery.js"></script>
    <!--you can write file in extra js file .. it depends on you -->
    <script type="text/javascript">
    $('#button').click(function(){
    // ask for confirmation
    var result = confirm("Want to delete?");

    // if it is confirmed
    if (result) {
    // get baseURL and ID using attributes
    var base_url = $('#button').attr('baseUrl');
    var postid  = $('#button').attr('postId');

    // make a ajax request
    $.ajax({
    url: base_url,
    type: "POST",
    dataType: 'json',
    success: function (data) {
        if(data){
                // Fade out the content id
                $('#content_id').closest(".todo-content").fadeOut();

        }       
    }
    });



    }

    });

    </script>

in controller:

// You just need to delete the post and return a status code of "200"
public function deletepost(){
        $id = $this->input->post('postid');
        $data = array('active' => 0);
        $this->Model_name->deletepost($id,$data);
        redirect('/abc/123');
    }

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