Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CodeIgniter中使用AJAX的Delete函数_Php_Ajax_Codeigniter - Fatal编程技术网

Php CodeIgniter中使用AJAX的Delete函数

Php CodeIgniter中使用AJAX的Delete函数,php,ajax,codeigniter,Php,Ajax,Codeigniter,问题已解决。。我会相应地更新我的代码。。谢谢大家 简介:我的代码显示消息跟踪数组(每条消息显示在灰色面板中)。用户可以通过单击删除按钮删除不需要的消息,该消息将在数据库中删除并从屏幕上消失 似乎我的删除功能不起作用。。谢谢你的建议。。我不知道。。这是我第一次尝试。提前谢谢 以下是我的代码: ajax代码: $(document).ready(function(){ $("body").on("click", "#responds .del_button", function(e) {

问题已解决。。我会相应地更新我的代码。。谢谢大家

简介:我的代码显示消息跟踪数组(每条消息显示在灰色面板中)。用户可以通过单击删除按钮删除不需要的消息,该消息将在数据库中删除并从屏幕上消失

似乎我的删除功能不起作用。。谢谢你的建议。。我不知道。。这是我第一次尝试。提前谢谢

以下是我的代码:

ajax代码:

$(document).ready(function(){
    $("body").on("click", "#responds .del_button", function(e) {
        e.preventDefault();
         var $btn = $(this),
             $li = $btn.closest('li');

        var traceId = $li.data('trace-id');

        jQuery.ajax({
                  type: 'post', 
                  url: 'traceDelete', 
                  dataType: 'text',

                  data: { 'traceId': traceId },

                  success: function (res) {
                      if (res === '1') {
                          $btn.fadeOut();
                      }
                  },
                  error: function (xhr, ajaxOptions, thrownError){
                      alert(thrownError);
                  }
        });
    });
部分视图代码:

<ul id="responds">
    <?php foreach ($line as $dataName => $contents){ ?>    
        <li data-trace-id="<?php echo $contents->trace_id; ?>">
            <div class="grey-panel" style="text-align:right">
                <div class="del_wrapper" id="<?php echo $contents->trace_id; ?>">
                    <a href="#" class="del_button" id="<?php echo $contents->trace_id; ?>" data-hover="tooltip" title="Delete this message <?php echo $contents->trace_id; ?>"  >
                        <i class="fa fa-times"></i>
                     </a>
                </div>
                <div class="col-sm-12 col-xs-12">
                  <?php print $contents->trace_hdr.'<br />';
                  print $contents->trace_src.'<br />';
                  print $contents->trace_dest.'<br />';
                  // some other things ?>
                </div>
            </div>
        </li> 
    <?php } ?>
</ul>
型号代码:

public function deleteTrace($id) {
    $this->db->where('trace_id', $id);
    $this->db->delete('trace_tbl');
    return $this->db->affected_rows() > 1 ? true:false;
}

首先,您使用的id属性是错误的。它们应该是唯一的,就像在一个元素中出现一样,而不是像您所做的那样多个。(同样,id属性不能以数字开头。)实际上不需要将它放在多个包含元素上,因为您可以使用jQuery的、、和轻松地遍历到父元素或子元素。然而,这不应该是你的问题的原因

现在,我认为问题的原因是您正在将id属性的第2个字符传递到AJAX请求中

$(document).ready(function(){
    $("body").on("click", "#responds .del_button", function(e) {
        e.preventDefault();
        // assign the ID e.g. "abc" to clickedID
        var clickedID = this.id; 
        // problem: assign the second character of your ID i.e. "b" into DbNumberID
        var DbNumberID = clickedID[1];
        // assign the same value to myData (which is redundant)
        var myData = DbNumberID; 

        $(this).hide(); 

        jQuery.ajax({
            type: "POST", 
            url: 'traceDelete', 
            dataType:"text", 
            // sending "myData=b"
            data: { myData: myData }, 
            success:function(traceDelete){
                alert("Deleted");
                $(DbNumberID).fadeOut();
            },
            error:function(xhr, ajaxOptions, thrownError){
                alert(thrownError);
            }
        });
});
我不再太熟悉CodeIgniter,但您需要从$\u请求或$\u POST数组中获取值,或者使用它们的值

编辑

这是未经测试的,但我会这样做

HTML

首先,让我们使用call它
数据跟踪id
。这将代替您对
id
属性的大量不当使用

<ul id="responds">
<?php foreach ($line as $dataName => $contents) : ?>    
    <li data-trace-id="<?php echo $contents->trace_id ?>">
        <div class="grey-panel" style="text-align: right">
            <div class="del_wrapper">
                <a href="#" class="del_button" data-hover="tooltip" title="Delete this message <?php echo $contents->trace_id ?>">
                    <i class="fa fa-times"></i>
                </a>
            </div>
            <div class="col-sm-12 col-xs-12">
                <?php echo $contents->trace_hdr ?><br />
                <?php echo $contents->trace_src ?><br />
                <?php echo $contents->trace_dest ?><br />
                <!-- etc -->
            </div>
        </div>
    </li> 
<?php endforeach ?>
</ul>
PHP

最后,我们从$u POST数组中获取值。检查是否有值:如果有,删除该项;否则就忽略它

public function traceDelete() 
{
    if ($traceId = $this->input->post('traceId')) {
        echo $this->model_general->deleteTrace($traceId) ? '1' : '0';
    }
    echo '0';
}

移除
var dbnumberrid=clickedID[1];var myData=dbnumberrid,因为它不需要。直接分配
数据:{myData:clickedID},
。还可以通过
POST
在控制器中获取
myData
的值

public function traceDelete() {

    $traceID = $this->input->post('myData');

    if($traceID)
        return $this->model_general->deleteTrace($traceID);

    return false;
}

firebug console for ajax中是否存在任何错误?我认为你的ajax会抛出500个错误。@kishor是的。。我点击了500个错误请用问题发布你的错误OK,然后你必须为你的Ajax提供正确的url。我认为我的javascript不起作用。。我没有得到myData的正确值。。不知道问题出在哪里。。因为我再也看不到任何错误了。。设法达到成功:功能(traceDelete)并获得警报“删除”,感谢您的精彩解释。。我从中了解到很多。。但是,它仍然不起作用。。跟踪ID为空,AJAX响应(res value)也为空,我已经调试并确认data-trace-ID中有值。。但不知何故,您没有将值传递给我,您过去的评论表明您有一个PHP错误。您是否也发布到正确的URL?除了
traceDelete
之外,URL是否应该有更多的内容,比如它前面的基本URL?事实上,您得到的是一个空白响应,这意味着您将无法访问请求处理程序,或者您的PHP中有一个错误。。所有数据都会相应地填充。。早些时候,我点击删除按钮后出现500错误。。但解决它…(这是一个打字错误)现在我得到data-trace-id的空白值。。我相信它会转到函数,否则,我将看不到该数据的跟踪id。。我错了吗?。。真的被困在这里了。。我想使用ajax,这样我就不必每次用户删除跟踪时都刷新页面。。嗯,我道歉。将
alert()
中的逗号更改为加号(如我的编辑中所示)。你应该可以看到这些值。我确实更改了它。。仍然没有捕获到traceID。。该值为空..此语句后的警报
clickedID
var clickedID=this.id
并检查是否得到任何值。使用
var clickedID=$(this.attr('id')并检查值是否出现。而不是
$(“body”)。在(“click”、“#responses.del_按钮”上,函数(e){
使用`$(“#responses.del_按钮”)。单击(函数(e){`。添加
var-clickedd=$(this.attr('id'))
然后尝试发出警报并查看。这样做..类似于Mikey的建议,但仍然无法删除跟踪
$(function() {
    $("#responds").on("click", ".del_button", function (e) {
        e.preventDefault();

        // keyword 'this' refers to the button that you clicked;
        // $(this) make a jQuery object out of the button;
        // always good idea to cache elements that you will re-using;
        var $li = $(this).closest('li');

        // get the associated trace ID from the list-item element;
        var traceId = $li.data('trace-id');

        alert('trace ID ' + traceId);

        // assuming you only want to hide the message only if it has been been successfully deleted from the DB?
        // if that is the case, then you have to wait for the AJAX response to tell you whether it was
        jQuery.ajax({
            type: 'post', 
            url: 'traceDelete', 
            dataType: 'text',
            // notice how I am using 'traceId'? on the server-side, the data will accessible by using $_POST['traceId'] or $this->input->post('traceId') 
            data: { 'traceId': traceId },
            // look at the response sent to you;
            // if I am not mistaken (boolean) true and false get sent back as (strings) 1 or 0, respectively;
            // so if the response is 1, then it was successfully deleted
            success: function (res) {
                alert('AJAX response ' + res);
                if (res === '1') {
                    alert('hiding button');
                    // hide the message
                    $li.fadeOut();
                }
            },
            error: function (xhr, ajaxOptions, thrownError){
                alert(thrownError);
            }
        });
    });
});
public function traceDelete() 
{
    if ($traceId = $this->input->post('traceId')) {
        echo $this->model_general->deleteTrace($traceId) ? '1' : '0';
    }
    echo '0';
}
public function traceDelete() {

    $traceID = $this->input->post('myData');

    if($traceID)
        return $this->model_general->deleteTrace($traceID);

    return false;
}