1

ユーザーが自分のお気に入りの本を追加または削除できる Web サイトがあります。ページのすべての行に「追加」と「削除」ボタンを追加しました。セッション配列にお気に入りの本を追加できますが、それについては問題ありません。しかし、「ユーザーのお気に入りの本リスト」から行を削除することはできません。

私はjquery、ajaxを使用しました。行の設定を解除することはできますが、セッションからその行を完全に削除して、もう一度注文する必要があります。行の設定を解除しても、セッションの要素番号は変わりません。

コードは次のとおりです。

    //There are rows in $newdata. That is to say, newdata array is not empty now.
    // I just add newdata array here to show that there is an array called newdata.

    var $newdata = array();

    function delete_row($rowid) {

    if (isset($rowid)) {

    $this->newdata = $this->session->userdata('contents');
    unset($this->newdata[$rowid]['id']);
    unset($this->newdata[$rowid]['name']);
    unset($this->newdata[$rowid]['author']);
    unset($this->newdata[$rowid]['year']);

    $this->session->set_userdata(array('contents' => $this->newdata));

    $contents = $this->session->userdata('contents');

    $i = 0;

        foreach ($contents as $key)         
        {
            if ($key['id'] != "") {
                $i++;

            $this->newdata[$i]['id'] = $key['id'];
            $this->newdata[$i]['name'] = $key['name'];
            $this->newdata[$i]['author'] =$key['author'];
            $this->newdata[$i]['year'] =$key['year'];

            } else {

            }
        }

    $this->session->set_userdata(array('contents' => $this->newdata));

    } else {

        echo "There is no row to be deleted";
    }
}

ここにビューコード(html)があります:

<div>

        <?php 

        if (count($contents)==0) {
        echo 'There is no favorite book';
        } else {

        echo '<table class="table table-striped">';

        $i = 0;
        echo count($contents);
        foreach ($contents as $items) {

        echo '<tr>';
        echo '<td>'.$items['id'].'</td>';
        echo '<td>'.$items['name'].'</td>';
        echo '<td>'.$items['author'].'</td>';
        echo '<td>'.$items['year'].'</td>';
        echo '<td><button id='.$i .' class="delete_row"><b>DELETE</b></button></td>';
        echo '</tr>';
        $i++;

    }
    echo '</table>';

    }
        ?>
    </div>

ここにjqueryコードがあります:

$('.delete_row').click(function(event) {
event.preventDefault();
var id = $(this).attr("id"); 
window.location.href = '/favorite/delete_row/'+id;

 });
4

1 に答える 1

1

問題はデルボタンIDにあると思います。$iの代わりに変数を使用しています$items['id']。そのため、JS リダイレクトはアイテムではなく count int をid削除ページに送信します。

変化する:

echo '<td><button id='. $i .' class="delete_row"><b>DELETE</b></button></td>';

の中へ:

echo '<td><button id='. $items['id'] .' class="delete_row"><b>DELETE</b></button></td>';
于 2012-08-04T12:17:48.587 に答える