循环形式

时间:2011-11-09 03:46:13

标签: php javascript html

我有几个问题。我正在桌子里面创建一个表单,据我所知,这不是一个好主意。但由于循环我想确保表头是在外面所以它不重复。有更聪明的方法吗?

另外更重要的是,我似乎无法通过删除按钮删除正确的视频。它似乎删除了列表中的最后一个。我如何循环这个有什么问题?

        <p>
        <h3>Recorded Videos</h3>
        <table id="webcam-table">
            <thead>
            <tr>
                <td>Camera Name</td>
                <td>Video Size</td>
                <td>Date Created</td>
                <td>Video Length</td>
                <td>Video Options</td>
            </tr>
            </thead>

        for($i=0;$i<$num_videos;$i++)
        {


<form action="<?php htmlentities($_SERVER['PHP_SELF']);?>" method="POST">
                <input type="hidden" name="video_id" value="<?php echo $result_videos[$i]["video_id"]; ?>" />

            <tbody>
                <tr>
                    <td>
                        <?php echo $result_videos[$i]["camera_name"]; ?>
                    </td>
                    <td>
                        <?php echo $result_videos[$i]["video_size"]; ?>
                    </td>
                    <td>
                        <?php echo $result_videos[$i]["video_datetime"]; ?>
                    </td>
                    <td>
                        <?php echo $result_videos[$i]["video_length"]; ?>
                    </td>
                    <td>
                        <input type="submit" name="delete_video" value="Delete" onClick="javascript:return confirm('Delete this video?');"/>
                    </td>
                </tr>
                </tbody>

        }

            echo "</table>";
            echo "</form>";
            echo "</p>";
    }
}

if (isset($_POST['delete_video'])) {

    $video_id = $_POST['video_id'];
    $query_delete_video = 'DELETE FROM `#__videos` WHERE `video_id`='.$video_id;
    $db->setQuery($query_delete_video);
    $db->query();
    header("location: " . $_SERVER['REQUEST_URI']);

3 个答案:

答案 0 :(得分:2)

在循环中,您将创建“表单”标记。但是你不是每次都关闭它。这导致您的删除问题。

移动

echo "</form>";

循环内部。

答案 1 :(得分:0)

对我来说很好看,我看到的唯一问题是标签应该在循环之外(之前打开,之后关闭)。

答案 2 :(得分:0)

修订代码

<?
if (isset($_POST['delete_video']))
{
    $video_id = $_POST['video_id'];
    $query_delete_video = 'DELETE FROM `#__videos` WHERE `video_id`='.$video_id;
    $db->setQuery($query_delete_video);
    $db->query();
    header("location: " . $_SERVER['REQUEST_URI']); //you should not allow things to be echoed before a header()
}
?>
<script type="text/javascript">
 function onMySubmit(video_id)
 {
    document.myform.video_id.value = video_id;
    return confirm('Delete this video?');
 }
</script>
<p>
<h3>Recorded Videos</h3>
               <!-- you had <?php htmlentities(...) ?>, you should have had
                    <?php echo htmlentities(...) ?> or <?= ... ?> -->
<form name="myform" action="<?= htmlentities($_SERVER['PHP_SELF']);?>" method="POST">
<input type="hidden" name="video_id" value="" />

<table id="webcam-table"> 
 <thead>
  <tr>
   <td>Camera Name</td>
   <td>Video Size</td>
   <td>Date Created</td>
   <td>Video Length</td>
   <td>Video Options</td>
  </tr>
 </thead>
 <tbody>
<? for($i=0;$i < $num_videos;$i++) { ?>
  <tr>
   <td><?= $result_videos[$i]["camera_name"]; ?></td>
   <td><?= $result_videos[$i]["video_size"]; ?></td>
   <td><?= $result_videos[$i]["video_datetime"]; ?></td>
   <td><?= $result_videos[$i]["video_length"]; ?></td>
   <td><input type="submit" name="delete_video" value="Delete" onClick="javascript:return onMySubmit(<?=$result_videos[$i]["video_id"];?>);"/></td>
  </tr>
<? } ?>
 </tbody>
</table>
</form>
</p>