使用javascript更改表单输入值
我试图更改隐藏表单的输入值,以更新数据库中的游戏分数 我在显示和玩游戏的php页面上有这个表单代码使用javascript更改表单输入值,javascript,php,forms,input,Javascript,Php,Forms,Input,我试图更改隐藏表单的输入值,以更新数据库中的游戏分数 我在显示和玩游戏的php页面上有这个表单代码 <form id ="recordForm" method="POST" action="updatePHP.php"> <input type='hidden' name="record" id='record' value='' /> </form> function postPHP(newRecord){ alert("POST TO
<form id ="recordForm" method="POST" action="updatePHP.php">
<input type='hidden' name="record" id='record' value='' />
</form>
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();
};
关于这个问题有很多话题,但我就是不知道我做错了什么。有什么建议吗?你应该试试
elem.value = newRecord;
我测试过,你的JS函数应该是这样工作的,你已经拥有的更少。我会删除这些提醒,因为你不再需要它们,并留下你的评论。这意味着JS函数不是问题所在
function postPHP(newRecord)
{
document.getElementById('record').value = newRecord;
document.getElementById('recordForm').submit();
};
在调用JS函数时不要忘记发送参数,我是用一个按钮完成的
<button onClick="postPHP('14')">Change</button>
所有这一切都会让你大开眼界谢谢你的建议!这是我有史以来的第一个问题,我会看看所有的问题,看看我是否能让它工作 这是我给HP打电话的地方:
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);
}
};
这里使用了一些访问方法
//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的主体中调用过它一次,因为某些原因,当它包含在头部时,它不能正确显示游戏
再次感谢你的帮助!我会让你知道我的工作内容 这就是我要做的
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();
};
函数postPHP(newRecord,seed){
警报(“发布到PHP”);
var internal=“”+
"";
document.getElementById('recordForm')。innerHTML=inner;
document.getElementById('recordForm').submit();
};
再次感谢所有的帮助,我只是不知道为什么第一种方法不起作用。这是我第一次尝试PHP和javascript。是的,我有很多建议……:)该代码应该可以正常工作(它会将值设置为
12
)。你到底有什么问题?您有任何错误吗?在哪里调用函数postPHP()
if (this.hasWon()) {
var finalScore = this.getScore();
var record = this.getRecord();
checkScore(finalScore, record);
return ret; //moving not allowed
}
//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;
}
;
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();
};