2011-11-21 43 views
0

我有一張簡單的表格,可以打印我們的視頻列表並在點擊該行時播放它們。我有一個刪除按鈕來刪除行中的相應視頻。但由於某種原因,當你點擊任何視頻時,它只會刪除列表中的最後一個。我可以查看頁面上的來源,並且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>"; 

?> 

回答

1

你能後的HTML?我懷疑你在頁面上有多個隱藏的輸入字段,全部使用相同的名字。如果是這種情況,只有在頁面上加載的最後一個纔會註冊爲有效的隱藏輸入。更好的方法是在刪除按鈕上設置一個數據屬性,然後在該刪除按鈕上添加一個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數據,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> 

希望有所幫助。祝你好運。

0

的問題是,隱藏字段的名稱不是唯一的(用於創建具有相同名稱的一個新的隱藏字段的循環每次迭代) 。如果腳本中的所有行都具有相同的名稱,該腳本應該如何知道您想要的行?

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 
    } 
} 
?> 
+0

啊我看到了,但我會在處理這個問題的代碼中做什麼? $ video_url = check_input($ _ POST ['video_url']); – Tom

+0

您可以檢查處理程序中以video_url開頭的值。 http://php.net/manual/en/reserved.variables.post.php –