表单隐藏值不工作
form hidden value not working?
我有一个简单的表,它打印我们的视频列表并在单击行时播放它们。我有一个删除按钮来删除行中相应的视频。但是由于某些原因,当你点击任何视频时,它只会删除列表中的最后一个。我可以看看页面上的源和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
}
}
?>
相关文章:
- PHP Javascript显示/隐藏按钮不工作
- ng隐藏和ng显示无法正常工作
- JavaScript-在手机上不工作的情况下,在外部单击时隐藏元素
- 隐藏uib弹出按钮点击不工作
- JQuery隐藏/显示无法正常工作
- 如何隐藏滚动条,但希望它继续工作
- 如何隐藏空<span>在IE中使用javascript.(注意,在其他浏览器中工作正常)
- 为什么竞争不能在离子中显示(隐藏在标题下方)(离子竞争不能正常工作)
- javascript()onclick隐藏元素-dos'我不在firefox工作
- JQuery隐藏选项没有'我在IE 11中不工作
- AngularJS:ng隐藏不工作
- 如何使多个javascript隐藏/显示在一个页面上工作
- JavaScript中的隐藏变量,它是如何工作的
- 如何使 Dreamweaver AP Div 显示/隐藏功能在 IE9 中工作
- Facebook自动收报机如何工作,例如在某些res或缩放它隐藏
- 显示/隐藏disqus按钮在火狐浏览器上无法正常工作
- 隐藏输入字段的内联 javascript 无法正常工作
- jquery设置隐藏的输入值在IE7和IE8中未按预期工作
- 密码隐藏/显示按钮只能单向工作
- jQuery 显示/隐藏缩放无法按预期工作