dongzhong7299 2014-09-08 17:07
浏览 49
已采纳

在html页面上显示数据后获取PHP行索引

I am getting data from a mysql DB using PHP and then displaying it inside a DIV on a web page. the data Im fetching is a list of task and I want the users to be able to delete a task once completed.

Image here:
http://www.picpaste.com/Tasks-esmvwRPX.jpg
(apparently I need 10 reputation points before I can embed a picture in my post!)

This is my PHP code used to display the date:

<section class="tasks">
        <div id="all-tasks">
            <fieldset class="tasks-list">
                <?php 
                    $strSQL = "SELECT Tasks.bolComplete, Tasks.strTask, Tasks.datDueDate FROM Tasks WHERE Tasks.idPeople ='"  . $_SESSION['UserID'] . "' AND Tasks.bolComplete ='0' ORDER BY Tasks.datDueDate";
                    $rs = mysql_query($strSQL);

                    while($row = mysql_fetch_array($rs)) {
                        echo '<label class="tasks-list-item left">';
                        if ($row['bolComplete'] == 1) {
                            echo '<input type="checkbox" name="task_1" value="' . $row['bolComplete'] . '" class="tasks-list-cb" checked>';
                        }
                        else {
                            echo '<input type="checkbox" name="task_1" value="' . $row['bolComplete'] . '" class="tasks-list-cb" unchecked>';
                        }
                        echo '<span class="tasks-list-mark"></span>';
                        echo '<span class="tasks-list-desc">' . $row['strTask'] . '</span>';

                        // Convert Date format 
                        $originalDate = $row['datDueDate'];
                        $newDate = date("d-m-y", strtotime($originalDate));
                        echo '<span class="tasks-list-date">' . $newDate . '</span>';
                        echo '</label>';
                        $i++;
                    }
                ?>
            </fieldset>
        </div>          
    </section>

How can I capture the index of the row displayed so I can do my delete query against the task that is selected?

I have a primary key on idTask (not displayed in the task list) which I need to capture for my delete statement.

Any help is greatly appreciated.

Thanks in advance. M.

  • 写回答

1条回答 默认 最新

  • duandian2725 2014-09-08 17:15
    关注

    you can assign you idTask to your lable i.e.

    <label id="idTaskXXX" class="tasks-list-item left">
    ...
    <lable>
    

    then where ever your code is which is checking for what row is clicked read the id field and use that for deletion

    here is an example of how to get id from element: click here

    <label  id="task1" value="Change" onclick="get_id(this.id)">
    Task1
    </label>
    <label  id="task2" value="Change" onclick="get_id(this.id)">
    Task2
    </label>
    

    javascript to get id:

    function get_id(id){
      alert('My Id is ' + id);
    }    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应