Tom
Tom

Reputation: 2634

loop through form

I have a couple of problems. I'm creating a form inside a table, from what I understand this is not a good idea. But because of the loop I want to make sure the table header is outside so it doesn't repeat. Is there a smarter way to do this?

Also more importantly I can't seem to get the delete button to remove the correct video. It seems to delete the last one in the list. Something wrong with how I'm looping over this?

        <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']);

Upvotes: 0

Views: 129

Answers (3)

Drew Chapin
Drew Chapin

Reputation: 7989

Revised code

<?
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>

Upvotes: 0

Aaron
Aaron

Reputation: 168

In your loop you are creating the 'form' tag. However you are not closing it each time. This is causing your deleting problem.

Move

echo "</form>";

Inside the loop.

Upvotes: 2

Karl Rosaen
Karl Rosaen

Reputation: 4644

looks good to me, the only issue I see is that the tag should be outside the loop (opening before, closing after).

Upvotes: 0

Related Questions