【问题标题】:Change form input value with javascript使用javascript更改表单输入值
【发布时间】:2013-12-11 09:33:35
【问题描述】:

我正在尝试更改隐藏表单的输入值以更新我的数据库中游戏的分数。

我在一个显示和玩游戏的 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();
};

关于这个主题有很多主题,但我只是无法弄清楚我做错了什么。有什么建议吗?

【问题讨论】:

  • 是的,我有很多建议..... :)
  • 该代码应该可以正常工作(它将值设置为12)。你的确切问题是什么?你有什么错误吗?
  • 你在哪里调用函数postPHP()

标签: javascript php forms input


【解决方案1】:

你应该试试

elem.value = newRecord;

【讨论】:

    【解决方案2】:

    你的 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替换成你的需要

    最后但同样重要的是,通过调用 print_r 方法检查您的 updatePHP.php

    print_r($_POST);
    

    所有这些都应该成功

    【讨论】:

      【解决方案3】:

      感谢您的所有建议!这是我有史以来的第一个问题,我将查看所有这些问题,看看我是否可以让它发挥作用。

      这是我调用 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);
              }
      };
      

      当用户将目标板条箱移回起始位置并执行以下语句时,将调用 checkScore

              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;
          }
          ;
      

      我确实包含了 javascript。我确实在 HTML 正文中调用过一次,由于某种原因,当包含在头部中时它无法正确显示游戏。

      再次感谢您的帮助!我会告诉你我要做什么!

      【讨论】:

        【解决方案4】:

        这就是我要做的工作。

        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。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-20
          • 1970-01-01
          • 1970-01-01
          • 2011-09-09
          • 1970-01-01
          • 2011-10-18
          相关资源
          最近更新 更多