【问题标题】:Javascript form submit POSTs no dataJavascript表单提交POST没有数据
【发布时间】:2015-03-11 04:25:52
【问题描述】:

这是我用来将数据发布到gamelobby.php的表单

<form id="game_form" method="POST" action="/gamelobby.php">
    <input type="text" id="plr_name"></input>
    <input type="hidden" value="" id="table_name"></input>
</form>
    <button id="codefight_create">Create a Fight</button>

这是提交表单的函数

    function codefight_create(){
            // blah blah, some ajax
            table_name = xmlhttp.responseText;
            document.getElementById("table_name").value = table_name;
            document.getElementById("game_form").submit();
            // console.log(table_name);
        }
    document.getElementById("codefight_create").addEventListener("click", function(){
        codefight_create();
    });

问题是,当我在gamelobby.php页面上var_dump($_POST)时,页面上的$_POST超全局中没有存储数据。

【问题讨论】:

  • 你能发布一些 ajax 等等吗?我怀疑这部分的问题
  • 不是,table_name 变量正是它应该的样子。

标签: php ajax forms post


【解决方案1】:

您忘记了input 的名称:

<input type="text" id="plr_name"></input>

<input type="text" id="plr_name" name="plr_name"></input>

table_nameinput 相同,id 仅在客户端,不会发送到服务器)

【讨论】:

    【解决方案2】:
    <form id="game_form" method="POST" action="/gamelobby.php">
    ...
    </form>
    <button id="codefight_create">Create a Fight</button>
    

    <form id="game_form" method="POST" action="gamelobby.php">
    ...
    <input type="submit" value="Create a flight"/> 
    </form>
    

    通过 onclick="codefight_create();" 添加点击事件在按钮输入上。 您可以使用侦听器事件,但需要在文档准备好时将其添加到 JQUERY 中。..

    【讨论】:

    • 我也试过了,它返回相同的结果。另外,我需要 javascript 功能。
    • 您只能使用添加监听事件,但您需要将其添加到document.ready中。
    • 就在&lt;/body&gt;结束标签之前,所以不需要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    相关资源
    最近更新 更多