形成隐藏价值不起作用?

时间:2011-11-21 21:15:49

标签: php javascript html

我有一个简单的表格可以打印我们的视频列表,并在您点击该行时播放它们。我有一个删除按钮来删除行中的相应视频。但出于某种原因,当您点击任何视频时,它只会删除列表中的最后一个视频。我可以查看页面上的源代码,video_url似乎是正确的。有什么想法吗?

以下是大部分代码:

<?php
// Dont allow direct linking
defined('_JEXEC') or die('Direct Access to this location is not allowed.');
//get current user
$user =& JFactory::getUser();
// get a reference to the database
$db = &JFactory::getDBO();

function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        echo $problem;
    }
    return $data;
}

if (isset($_POST['delete_video'])) {
    $video_url = check_input($_POST['video_url']); //value here is always just the last row in the table!!!
    echo $video_url;
    $query_delete_video = "DELETE FROM `#__videos` WHERE `video_url`='$video_url'";
    $db->setQuery($query_delete_video);
    $db->query();
}

//list all details of the camera including camera_name
$query_videos = "SELECT #__videos.video_url, #__videos.video_size, #__videos.video_datetime, #__videos.video_length, #__cameras.camera_name
    FROM #__videos INNER JOIN #__cameras using (user_id, camera_id) 
    WHERE #__videos.user_id=".$user->id." ORDER BY #__videos.video_datetime DESC";
$db->setQuery($query_videos);
//get number of cameras so we can build the table accordingly
$db->query();
$num_videos = $db->getNumRows();
// We can use array names with loadAssocList.
$result_videos = $db->loadAssocList();


echo "<html>";
echo "<head>";
?>
<link href="recordings/recordings.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="flowplayer/example/flowplayer-3.2.6.min.js">  </script>
<?php

echo "</head>";
echo "<body>";
?>

<?php
if (!isset($result_videos))
{
    //TODO check if query failed
}
else 
{
    if ($num_videos == 0)
    {           
?>
        <div id="cc_table">
        <table id="webcam-table">
            <thead>
            <tr>
                <th>Camera Name</th>
                <th>Camera Details</th>
                <th>Video Options</th>
            </tr>
            </thead>
            <td colspan=3><b><i><center>You currently have no videos created. Start monitoring today!</center></i></b></td>
        </table>
        </div>
<?php
    }
    else
    {       
?>


        <div id="cc_table">
        <form name="myform" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST">

        <table id="webcam-table">
            <thead>
            <tr>
                <th>Camera Name</th>
                <th>Camera Details</th>
                <th>Video Options</th>
            </tr>
            </thead>
            <tbody>

<?php

        for($i=0;$i<$num_videos;$i++)
        {
?>              
                <tr onclick="DoNav('<?php echo $result_videos[$i]["video_url"]; ?>');">
                    <td>
                        <?php echo $result_videos[$i]["camera_name"]; ?> 
                    </td>
                    <td>
                        Date Created: <?php echo $result_videos[$i]["video_datetime"]; ?> <br>
                        Video Size: <?php echo $result_videos[$i]["video_size"]; ?> bytes <br>
                        Video Length: <?php echo $result_videos[$i]["video_length"]; ?> secs
                    </td>
                    <td>

                        <input type="submit" name="delete_video" value="Delete" onClick="return confirm('Are you sure you want to delete?')"/>
                    </td>
                </tr>
                <input type="hidden" name="video_url" value="<?php echo $result_videos[$i]["video_url"]; ?>" />
<?php

        }
            echo "</tbody>";
            echo "</table>";
            echo "</form>";
            echo "</div>";
    }
}

?>

<div id="player" style="display:block;width:320px;height:240px;background-image:url(recordings/landscape.jpg)"></div>

<script type="text/javascript">
function DoNav(theUrl)
{
//document.write(document.location.href = theUrl);
flowplayer("player", "flowplayer/flowplayer-3.2.7.swf", theUrl);
}
</script>

<?php

echo "</body>";
echo "</html>";

?>

3 个答案:

答案 0 :(得分:1)

你可以发布HTML吗?我怀疑你在页面上有多个隐藏的输入字段,都是同名的。如果是这种情况,只有在页面上加载的最后一个将注册为有效的隐藏输入。更好的方法是在删除按钮上设置数据属性,然后将JS click事件添加到该删除按钮。像这样:

<button class="delete" data-video_url="youtube.com/abcd">Delete ABCD</button>
<button class="delete" data-video_url="vimeo.com/xyz">Delete XYZ</button>

然后当有人点击.delete按钮时,您会通过POST将data-video_url值发送到PHP脚本。 jQuery示例可能如下所示:

$('.delete').click(function() {
    var url = $(this).data('video_url');
    $.post('/delete.php', { video_url: video_url }, function() {
        // Do something on success
    });
});

另一种方法是简单地将每个按钮设为自己的形式:

<form method="post" action="delete.php">
   <input type="hidden" name="video_url" value="url_goes_here">
   <button>Delete</button>
</form>

希望有所帮助。祝好运。

答案 1 :(得分:0)

问题是隐藏字段的名称不是唯一的(对于循环中的每次迭代,都会创建一个具有相同名称的新隐藏字段)。如果所有行都具有相同的名称,脚本应该如何知道你想要哪一行?

答案 2 :(得分:0)

您的代码似乎没有正确命名隐藏值。

您应该使用$ i来命名变量。

input type="hidden" name="video_url<?php echo $i; ?>"

然后在处理程序中你可以做

<?php
if ($_POST) {
  $kv = array();
  foreach ($_POST as $key => $value) {
    // check here which one begins with video_url
  }
}
?>