【问题标题】:codeigniter sending a variable from ajax to controllercodeigniter 将变量从 ajax 发送到控制器
【发布时间】:2015-09-20 12:02:58
【问题描述】:

我目前正在做一个 ajax 添加、更新和删除。我想我会从删除开始,因为它是最简单的,希望它可以在其他方面对我有所帮助。

在 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,
  });
}

在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>

在控制器中

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

这已经在工作了,但是我打算将 crud 制作成 ajax。我正在尝试将 postid 从 ajax 传递到控制器以删除此帖子。 fadeout 已经有效,但只有 ajax 无效。我对 ajax 很陌生,所以我不知道我哪里出错了,我可能还会再次询问有关 crud 其他部分的问题。

【问题讨论】:

    标签: javascript php jquery codeigniter


    【解决方案1】:

    已修复!

    问题出在$.ajax 中的网址。它返回一个垃圾。

    所以我在标题中添加了一个脚本

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

    只需在url: 中使用BASE_URL,就像url: BASE_URL+'classes/deletepost', 一样

    【讨论】:

      【解决方案2】:

      请尝试遵循这个:

      在 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>
      

      在控制器中:

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

      【讨论】:

      • 我已经修好了。问题是 ajax 中的url。它返回一些垃圾。在标头中声明 base_url 似乎更容易。
      • 哇……太棒了!一切顺利! :)
      猜你喜欢
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      相关资源
      最近更新 更多