2013-01-07 104 views
0

可能重複:
pass php variable to javascript如何通過JavaScript變數PHP

我想比較與保存在數據庫中的數據的用戶點擊的點擊數(S)。 我不知道如何繼續傳遞HTML「點擊」的價值來與PHP中的「計數」進行比較。

<html> 
<script type="text/javascript"> 

var count = 0; 

function countClicks() 
{   
    count = count + 1; 
    document.getElementById("clicks").innerHTML = count; 
} 
</script> 

<?php 
    if(empty($counts)){ 
?> 

<script type="text/javascript"> 

    alert("Count is empty!!"); 

</script> 

<?php 

} else { 

$data = mysql_query("SELECT clicks FROM customerdetails WHERE customer_username='$username'"); 
$info = mysql_fetch_array($data); 
//compare $data with the clicks 
echo 'same!'; 
} 
?> 

<body> 
<a href="javascript:countClicks();">Count Clicks</a> 
<input type="button" onclick=countClicks() value="Click"/> 
<p id="clicks">0</p> 
</body> 
</html> 
+0

請不要使用'mysql_ *'函數來編寫新代碼。他們不再維護,社區已經開始[棄用程序](http://goo.gl/KJveJ)。請參閱* [紅盒子](http://goo.gl/GPmFd)*?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定哪些,[這篇文章](http://goo.gl/3gqF9)會幫助你。如果你選擇PDO,[這裏是很好的教程](http://goo.gl/vFWnC)。 –

回答

2

您以錯誤的方式使用PHP和Javascript。 PHP是一種服務器端語言。這意味着它在頁面甚至在瀏覽器上加載之前運行。

您將不得不創建一個JavaScript點擊計數器並將其值放入一個隱藏的表單域。然後使用提交按鈕將信息發送到服務器(PHP)。然後讓PHP從數據庫中進行檢查和選擇並返回一個答案。

另一種解決方案是使用JavaScript AJAX,但我建議先嚐試以上。

0

繼續進行的最好方法是創建一個Asynchronous JavaScript and XML調用(AJAX)。 PHP是服務器端語言,其中之前執行HTML(因此,在Javascript之前)被構建並向瀏覽器顯示。

爲此,爲的Javascript的唯一辦法用PHP來交換變量和數據是使一個AJAX調用(你總是可以重新加載頁面的表單提交或session variablescookies,但這不是最好的去的方式,如果重複動作過於頻繁。

在Ajax中,你可以讓另一個PHP頁面,將檢查這兩個值,返回任何你想要的。響應可以存儲在一個Javascript變量,甚至JSON

我建議您閱讀關於AJAX的更多信息,並瞭解what is PHP how to use it


編輯:在閱讀您的評論後,我決定在這裏寫一個簡單的例子。

的JavaScript(在HTML頁面中)

var xmlhttp; 

if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.onreadystatechange=function() 
{ 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    /*Here you should do what you want. 
     xmlhttp.responseText is the content of your PHP answer! */ 

    var result = xmlhttp.responseText; 
    //I am parsing a JSON response, which is a specific, universal language 
    //To exchange data without losing formatting or anything else. 
    result = JSON.parse(result); 

    /* The we use the name of our PHP array key as a property 
     Here it is "response" (see PHP json_encode line)  */ 
    alert(result.response); 
 } 
} 
/* Change this URL for the PHP filename. we use the GET method to send data. */ 
/* You should always use the POST method when sending sensitive data */ 
xmlhttp.open("GET","getUserClicks.php?clicks="+count+"&username="+username,true); 
xmlhttp.send(); 

PHP(這是一個名爲getUserClicks.php文件)

<?php 
if(!isset($_GET['username']) || !isset($_GET['clicks'])) 
    die("Error"); 

$username = $_GET['username']; 
$jsClicks = $_GET['clicks']; 
$phpClicks = null; 

#I am using the mysqli class to execute the query since mysql is deprecated in PHP5. 
$data = mysqli_query("SELECT clicks FROM customerdetails WHERE customer_username='$username'"); 

while($row = mysqli_fetch_array($data)) 
{ 
    $phpClicks = $row['clicks']; 
} 

#If your "IF" only contains one line, you don't need brackets. 
#Otherwise they are needed. 

if($phpClicks == null) 
    die("Could not get the number of clicks of the desired username"); 

#This is the string PHP will send to Javascript. 
$response = "Same number of clicks!"; 

#If phpClicks is different and has the same type as jsClicks... 
if($phpClicks !== $jsClicks) 
{ 
    $response = "Number of clicks changed!"; 
    if($jsClicks > $phpClicks) 
    { 
     #Updates the number of clicks the user has done. 
     $mysqli_result = mysqli_query("UPDATE customerdetails SET clicks=$jsClicks WHERE customer_username='$username';"); 
    } 
} 

echo json_encode(array('response'=>$response)); 

?>

一定要如果你看到功能或方法,你不知道他們在做什麼(例如:isset)。

+0

嗨,吉利德。感謝您的建議。我在w3schools中通過了AJAX的教程。您能否詳細解釋一下詳細信息,我該如何解決AJAX問題?我很抱歉,因爲我在編程方面很弱。然而,我正在努力學習。 通過使用AJAX,我應該分開JavaScript和PHP文件? 然後,運行php文件,使用AJAX調用javascript中的函數?如: xmlhttp.open(「GET」,「click.html」,true); ? 那麼我該如何檢索JavaScript中的「點擊」,並與我的數據庫中的數據進行比較? – user1942429

+0

我正在爲你做一個例子。一旦完成,我會編輯我的文章。 –

+0

非常感謝你〜我正在盡我所能去做..但我無法弄清楚我應該在哪裏劃分部件。喜歡哪個函數,其中.php或.html〜*困惑* – user1942429