简体   繁体   English

Codeigniter从ajax向控制器发送变量

[英]codeigniter sending a variable from ajax to controller

I'm currently doing an ajax add,update and delete. 我目前正在做ajax添加,更新和删除。 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) 在jquery中(这在$ doc.ready内部,并且事件已正确触发)

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 在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. 这已经在起作用,但随后我计划将其添加到ajax中。 I'm trying to pass the postid from ajax to controller to delete this post. 我正在尝试将postid从ajax传递到控制器以删除此帖子。 The fadeout already works but only the ajax does not. fadeout已经可以使用,但只有Ajax无效。 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. 我对ajax还是很陌生,所以我不知道我要去哪里错了,我可能还会再问有关crud其他部分的问题。

Fixed! 固定!

The problem was the url inside the $.ajax . 问题是$.ajax的url。 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', 只需在url:使用BASE_URL url:就像url: BASE_URL+'classes/deletepost',

Please Try to follow this: 请尝试遵循以下步骤:

In Codeigniters View: 在Codeigniters视图中:

<!-- 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');
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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