使用js数组push()向我的表添加DELETE链接

时间:2017-05-21 11:49:48

标签: javascript php mysql slim database-table

我想在我的表格中加入一个删除按钮的链接。然后这将 - onclick - 从我的数据库中删除,具体取决于ID。

我搜索了堆栈溢出没有占上风。正确方向上的一点很棒(链接,代码建议等等。)。

HTML:

<h5 class="tabletext"><a href="#">View Markers</a></h5>

     <form action="" method="post">
        <table class="table table-bordered">
           <thead>
              <tr>
                <th data-field="id">ID</th>
                <th data-field="type">Type</th>
                <th data-field="area">Area</th>
                <th data-field="area">Weight in lbs</th>
                <th data-field="area">Length in cm</th>
                <th data-field="area">Latitude</th>
                <th data-field="area">Longitude</th>
                <th data-field="area">DELETE</th>                     
              </tr>
           </thead>
         </table>
      </form>  

.js:

    $.ajax({
        type: 'GET',
        dataType: "json",
        url: "rest/rest/api.php/markerss",
        success: showMarkerTable,
        error: showError
        });

function showMarkerTable(responseData) {

        var items = [];
    $.each(responseData.MARKERS, function(key, val){
        items.push("<tr>");
        items.push("<td id=''"+key+"''>"+val.id+"</td>");
        items.push("<td id=''"+key+"''>"+val.area+"</td>");
        items.push("<td id=''"+key+"''>"+val.type+"</td>");
        items.push("<td id=''"+key+"''>"+val.weight+"</td>");
        items.push("<td id=''"+key+"''>"+val.length+"</td>");
        items.push("<td id=''"+key+"''>"+val.lat+"</td>");
        items.push("<td id=''"+key+"''>"+val.lng+"</td>");

        //The delete button here
        items.push("<td id=''"+key+"''>"+val.delete+"</td>");
        items.push("</tr>");
    });
    $("<tbody/>", {html: items.join("")}).appendTo("table");

    }

function showError(){
    alert("Sorry, there was a problem!");
}

使用SLIM框架检索数据的PHP:

function getTable() {
        $sql="select * FROM markers ORDER BY id";

    try{
        $db = getConnection();
        $stmt = $db->query($sql);
        $markers = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;

        responseJson('{"MARKERS":'.json_encode($markers).'}',200);
        }

        catch(PDOException $e){
        responseJson('{"error":{"text":'.$e->getMessage().'}}',500);
        }
}

0 个答案:

没有答案