表单隐藏值不工作

form hidden value not working?

本文关键字:工作 隐藏 表单      更新时间:2023-09-26

我有一个简单的表,它打印我们的视频列表并在单击行时播放它们。我有一个删除按钮来删除行中相应的视频。但是由于某些原因,当你点击任何视频时,它只会删除列表中的最后一个。我可以看看页面上的源和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>";
?>

你可以张贴HTML吗?我怀疑您在页面上有多个隐藏的输入字段,它们都具有相同的名称。如果是这种情况,那么只有最后一个加载到页面上的输入才会注册为有效的隐藏输入。更好的方法是在删除按钮上设置一个data属性,然后向该删除按钮添加一个JS单击事件。像这样:

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

希望有帮助。祝你好运。

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

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

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

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

那么在handler中你可以做

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