2013-12-11 61 views
-1

我正在嘗試更改隱藏表單的輸入值,以更新數據庫中游戲的分數。使用javascript更改表單輸入值

我在顯示和玩遊戲的php頁面上有這個表單代碼。

<form id ="recordForm" method="POST" action="updatePHP.php"> 
     <input type='hidden' name="record" id='record' value='' /> 
</form> 

而我試圖用這個javascript來改變隱藏輸入字段的值。這是在控制遊戲的單獨的JavaScript文件中。

function postPHP(newRecord){ 
alert("POST TO PHP"); //test to make sure I am calling this function 
alert (newRecord); //alerts the correct value 

var elem = document.getElementById('record'); 
elem.value = 12; 
// document.getElementById('record').value = newRecord; 
// document.getElementById('recordForm').submit(); 
}; 

在這個問題上有很多話題,但我只是無法弄清楚我做錯了什麼。有什麼建議麼?

+0

是的,我有很多建議..... :) – P5Coder

+0

該代碼應該可以正常工作(它會將值設置爲「12」)。你確切的問題是什麼?你有什麼錯誤嗎? – musefan

+0

你在哪裏調用函數'postPHP()' – Roopendra

回答

0

你應該嘗試

elem.value = newRecord; 
0

你的JS函數應該像這樣,我測試,更少了什麼,你已經有了。我刪除警報,因爲你不再需要它們,並留下你所評論的內容。這意味着你的JS函數不是問題。

function postPHP(newRecord) 
{ 

document.getElementById('record').value = newRecord; 
document.getElementById('recordForm').submit(); 

}; 

調用JS功能時,不要忘了發送的參數,我用一個按鈕做的

<button onClick="postPHP('14')">Change</button> 

因爲你的JS函數是在一個單獨的文件,不要忘記把它列入在調用函數文件

<head> 
    <script type="text/javascript" src="PATH/exampleName.js"></script> 
</head> 

更換上述標籤的src您的需要

最後,但與呼叫並非最不重要的檢查updatePHP.php到的print_r

print_r($_POST); 

所有應該做的伎倆

0

感謝您的所有建議的方法!這是我有史以來的第一個問題,我會看看他們,看看我能否得到它的工作。

這是我打電話postPHP:

function checkScore(score, record) {  

     alert('Score= ' + score); 
     alert ('Record= '+ record); 

     if(score < record || record === 0){ 
      alert ("NEW RECORD"); //this alert is displayed when needed 
      postPHP(score); 
     } 
}; 

而當用戶移動的目標箱子回到開始點下面的語句被執行

 if (this.hasWon()) { 
      var finalScore = this.getScore(); 
      var record = this.getRecord(); 
      checkScore(finalScore, record); 
      return ret;  //moving not allowed 
     } 

有checkScore被稱爲和在那裏使用一些訪問方法。

//access methods 
    Board.prototype.hasWon = function() { 
     return state === 1; 
    }; 
    Board.prototype.getScore = function() { 
     return score; 
    }; 
    Board.prototype.getWt = function(r, c) { 
     return b[r][c]; 
    }; 
    Board.prototype.getData = function() { 
     return {"bobR": bobR, "bobC": bobC, "bobDir": bobDir, 
      "tgtR": tgtR, "tgtC": tgtC, 
      "startC": startC, "n": n}; 
    }; 
    Board.prototype.getRecord = function(){ 
     var s = "" + window.location; 
     var ampIdx = "" + s.indexOf("&"); 
     ampIdx = parseInt(ampIdx); 
     ampIdx = ampIdx + 7; 

     var record = "" + s.substring(ampIdx); 
     //alert("Puzzle Record= " + record); 
     record = parseInt(record); 
     return record; 
    } 
    ; 

我確實包括了javascript。我在HTML的主體中稱它爲一次,因爲某些原因,當它包含在頭部時它不會正確顯示遊戲。

再次感謝您的幫助!我會讓你知道我的工作!

0

這就是我的工作。

function postPHP(newRecord, seed) { 
    alert("POST TO PHP"); 
    var inner = "<input type='hidden' name='record' id='record' value=" + newRecord + " >"+ 
       "<input type='hidden' name='seed' id='seed' value=" + seed + " >"; 
    document.getElementById('recordForm').innerHTML = inner; 
    document.getElementById('recordForm').submit(); 
}; 

再次感謝所有的幫助,我只是不知道爲什麼第一種方法不起作用。這是我第一次嘗試PHP和JavaScript。