形成隐藏的价值不工作? [英] form hidden value not working?

查看:58
本文介绍了形成隐藏的价值不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以下是大部分代码:

  <?php 
//不允许直接链接
defined('_ JEXEC')或者死掉('不允许直接访问这个位置'。
//获取当前用户
$ user =& JFactory ::的getUser();
//获得对数据库的引用
$ db =& JFactory :: getDBO();

函数check_input($ data,$ problem ='')
{
$ data = trim($ data);
$ data = stripslashes($ data);
$ data = htmlspecialchars($ data);
if($ problem&& strlen($ data)== 0)
{
echo $ problem;
}
返回$ data;
}

if(isset($ _ POST ['delete_video'])){
$ video_url = check_input($ _ POST ['video_url']); //这里的值总是表格中的最后一行!
echo $ video_url;
$ query_delete_video =DELETE FROM`#_​​_ videos` WHERE`video_url` ='$ video_url';
$ db-> setQuery($ query_delete_video);
$ db-> query();
}

//列表中的相机的所有细节,包括camera_name
$ query_videos =SELECT #__ videos.video_url,#__ videos.video_size,#__ videos.video_datetime,#__videos。 video_length,#__ cameras.camera_name
从#__videos INNER使用(USER_ID,camera_id)
JOIN #__cameras WHERE #__ videos.user_id = $用户> ID ORDER BY #__ videos.video_datetime DESC ;
$ db-> setQuery($ query_videos);
//获取相机数量,以便我们可以相应地构建表格
$ db-> query();
$ num_videos = $ db-> getNumRows();
//我们可以将数组名与loadAssocList一起使用。
$ result_videos = $ db-> loadAssocList();


echo< html>;
echo< head>;
?>
< link href =recordings / recordings.css =stylesheettype =text / css/>
< script type =text / javascriptsrc =flowplayer / example / flowplayer-3.2.6.min.js> < /脚本>
<?php

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

<?php
if(!isset($ result_videos))
{
// TODO检查查询是否失败
}
else
{
if($ num_videos == 0)
{
?>
< div id =cc_table>
< table id =webcam-table>
< thead>
< tr>
< th>相机名称< / th>
< th>相机详情< / th>
< th>视频选项< / th>
< / tr>
< / thead>
< td colspan = 3>< b>< i>< center>您目前没有创建视频。立即开始监控!< / center>< / i>< / b>< / td>
< / table>
< / div>
<?php
}
else
{
?>


< div id =cc_table>
< form name =myformaction =<?php echo htmlentities($ _ SERVER ['REQUEST_URI']);?>方法= POST >

< table id =webcam-table>
< thead>
< tr>
< th>相机名称< / th>
< th>相机详情< / th>
< th>视频选项< / th>
< / tr>
< / thead>
< tbody> ($ i = 0; $ i <$ num_videos; $ i ++)
{
?>

<?php


< tr onclick =DoNav('<?php echo $ result_videos [$ i] [video_url];?>');>
< td>
<?php echo $ result_videos [$ i] [camera_name]; ?>
< / td>
< td>
创建日期:<?php echo $ result_videos [$ i] [video_datetime]; ?> <峰; br>
视频大小:<?php echo $ result_videos [$ i] [video_size]; ?>字节< br>
视频长度:<?php echo $ result_videos [$ i] [video_length]; ?>秒
< / td>
< td>

< input type =submitname =delete_videovalue =DeleteonClick =return confirm('您确定要删除?')/>
< / td>
< / tr>
< input type =hiddenname =video_urlvalue =<?php echo $ result_videos [$ i] [video_url];?> />
<?php

}
echo< / tbody>;
echo< / table>;
回声< / form>;
回显< / div>;
}
}

?>

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

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

<?php

echo< / body>;
回显< / html>;

?>


解决方案

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

 < button class =deletedata-video_url =youtube.com/abcd>删除ABCD< /按钮> 
< button class =deletedata-video_url =vimeo.com/xyz>删除XYZ< / button>

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

  $('。delete')。click(function(){
var url = $(this).data('video_url');
$ .post('/ delete.php',{video_url:video_url},function(){
//成功
});
});

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

 < form method =postaction =delete.php> 
< input type =hiddenname =video_urlvalue =url_goes_here>
<按钮>删除< /按钮>
< / form>

希望有所帮助。祝你好运。

I have a simple table that prints our the list of videos and plays them when you click on the row. I have a delete button to delete the corresponding video in the row. But for some reason when you click on any video it will only delete the last one in the list. I can look at the source on the page and the video_url seems to be correct. Any ideas?

Here is the majority of the code:

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

?>

解决方案

Can you post the HTML? I suspect you've got multiple hidden input fields on the page, all with the same name. If that's the case, only the last one that loads on the page will register as a valid hidden input. A better way to do it would be to set a data attribute on the delete button, and then add a JS click event to that delete button. Something like this:

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

and then when someone clicks on a .delete button, you send the data-video_url value to the PHP script via POST. A jQuery example might look like this:

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

Another way to do it is to simply make each button its own form:

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

Hope that helps. Good luck.

这篇关于形成隐藏的价值不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆