2015-08-24 20 views
-1

現在我已經編程在PHP中,每擊中一個按鈕後,你做,但多數民衆贊成在PHP中,所以在點擊按鈕後,程序與接觸服務器。 我想知道是否有可能通過javascrit使命中計算sothat程序不升級到服務器的命中將在屏幕上可見,並通過JavaScript每次升級。在結束洞之後,我可以將結果保存到服務器。 我做了以下測試。但電腦仍然通過服務器進行更新。即時製作一個高爾夫記分卡程序在PHP中,但要使用JavaScript來計數點擊

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<? 
if ($aantalslagen_plus==""){ 
    $aantalslagen_plus="0"; 
} 
?> 
    <script type="text/javascript"> 
var a = <? echo $aantalslagen_plus; ?> 
     function bereken() { 
    a += 1; 
    document.getElementById('aantalslagen').value = a; 
}; 
</script> 

</head> 
<body> 
<form method="post" action="" . $_SERVER['PHP_SELF'] . ""> 
<br><br><br> 
<table width="422" height="179" border="0"> 
<tr> 
<td>totaal aantal slagen: </td> 
<td>&nbsp;</td> 
</tr> 
<?php 
    $aantalslagen_plus = htmlentities($_POST['aantalslagen_plus']); 
?> 
<tr> 
<td>totaal aantal slagen php:</td> 
<td> 
<? echo "<input type=\"text\" VALUE=\"$aantalslagen_plus\" name=\"aantalslagen_plus\" id=\"aantalslagen\" size=\"10\">"; ?> 
<td><? echo $aantalslagen_plus; ?></td> 
</tr> 
</table> 
<br> 
<button onclick="bereken();"> + </button> 
</form> 
</body> 
</html> 
+0

是的,那是可能的。我假設你知道PHP需要一個web服務器來執行。如果你想要一個不需要與服務器交互的網頁來運行(一旦它被加載),你就不能使用PHP。 你可能想看看你正在計劃的「localstorage」。 – Burki

回答

0

只是初始化aantalslagen在JavaScript 0和不惹任何<form>或POST請求或類似的東西,因爲你並不需要,因爲它似乎並不像你保存的值一些cookie,因此如果用戶刷新頁面,值將保留。如果你希望值保留一次用戶刷新,我建議你看看localStorage

//The input element: 
 
var input = document.getElementById("aantalslagen-input"); 
 
//The td element: 
 
var td = document.getElementById("aantalslagen-td"); 
 
//The button element: 
 
var button = document.getElementById("aantalslagen-button"); 
 
//The variable: 
 
var aantalslagen = 0; 
 

 
//When the button is clicked... 
 
button.addEventListener("click", function() { 
 
    //Increment aantalslagen: 
 
    aantalslagen += 1; 
 
    //Update it in the input and td: 
 
    input.value = aantalslagen; 
 
    td.textContent = aantalslagen; 
 
});
<br><br><br> 
 
<table width="422" height="179" border="0"> 
 
<tr> 
 
<td>totaal aantal slagen: </td> 
 
<td>&nbsp;</td> 
 
</tr> 
 
<tr> 
 
<td>totaal aantal slagen php:</td> 
 
<td><input type="text" VALUE="0" name="aantalslagen_plus" id="aantalslagen-input" size="10"> 
 
<td id="aantalslagen-td">0</td> 
 
</tr> 
 
</table> 
 
<br> 
 
<button id="aantalslagen-button"> + </button>

0

<button><form>元素的內部,默認的行爲是對的形式提交給服務器。

刪除<form>元素,因爲它不再用於任何目的。

相關問題