2015-02-07 59 views
-2

我試圖在單擊表中的鏈接時運行對mysql的更新。 對於這個我做了3個文件:從javascript運行php更新文件

movies.php

<html> 
 
<head> 
 
<link href="css/style.css" rel="stylesheet" type="text/css" media="screen" /> 
 
<script src="video.js" type="text/javascript"></script> 
 
</head> 
 

 
<?php 
 

 
include 'combo_new.php'; 
 
include 'config.php'; 
 
include 'opendb.php'; 
 

 
$ndate = $_POST['ndate']; 
 

 
$result = mysql_query("SELECT * 
 
      FROM DayMovie 
 
      WHERE FileDate LIKE '$ndate%' ORDER BY FileDate DESC") 
 
or die(mysql_error()); 
 
echo "<table border='0'>"; 
 
echo "<tr> <th>Dato</th><th>Visninger</th><th>Handling</th></tr>"; 
 
while($row = mysql_fetch_array($result)) { 
 
\t echo "<tr><td>"; 
 
\t echo date('d.m.Y', strtotime($row['FileDate'])); 
 
\t echo "</td><td>"; 
 
\t echo $row['Counter']; 
 
\t echo "</td><td>"; 
 
\t echo "<a href='alldaymovies/{$row['FileName']}' onclick='playVideo(this.href, {$row['FileName']});' onkeypress='playVideo(this.href, {$row['FileName']});'>Se film</a>"; 
 
\t echo "</td></tr>";   
 
} 
 
echo "</table>"; 
 

 
include 'closedb.php'; 
 
?> 
 

 
</html>

的Video.js

function playVideo(filename) 
 
{ 
 
\t $.post("update.php" {"filename":filename}, 
 
\t function(data) { 
 
\t alert("Data Loaded: " + data); 
 
}); 
 
}

update.php

<?php 
 

 
include 'config.php'; 
 
include 'opendb.php'; 
 

 
$filename = $_POST['filename']; 
 

 
$result = mysql_query("UPDATE DayMovie SET Counter=Counter+1 WHERE FileName='$filename'") 
 
or die(mysql_error()); 
 

 
include 'closedb.php'; 
 
?>

但是有件事不正確的在這裏...任何人都可以看到,我錯了?

+0

你有什麼問題?你得到哪些錯誤?什麼不工作? – 2015-02-07 09:55:38

回答

2

問題可能是您的用戶在調用update.php之前已經重定向到其他頁面。請記住,如果您將瀏覽器重定向到另一個忙於取消請求的頁面。

要測試這是否真的是問題,請嘗試用「#」替換「a」元素的href。 ,改變你的playVideo功能看起來像這樣:

function playVideo(filename) 
 
{ 
 
\t $.post("update.php" {"filename":filename}, 
 
\t function(data) { 
 
\t alert("Data Loaded: " + data); 
 
    setTimeout(function(){ document.location.href="alldaymovies/" + filename;}, 300); 
 
}); 
 
}