【问题标题】:Cannot POST value to other php?无法将值发布到其他 php?
【发布时间】:2017-03-19 02:15:47
【问题描述】:

我的表中有一个按钮,原来是使用 GET 将一些变量发送到新页面 user.php ,但我不想在 url 中显示这些信息。 我尝试构建一个表单并将其提交给另一个 user.php,但 user.php 什么也没得到:

 $('table').on( 'click', 'td .btn-info', function () {
    var parent = $(this).closest('table').parents('tr').prev();
    var parentIndex = parent.find('.index').text();
    var currentIndex = $(this).closest('tr').index();
    var data = sections[parentIndex][currentIndex];

     var mapForm = document.createElement("form");
      mapForm.target = "Map";
      mapForm.method = "POST"; // or "post" if appropriate
      mapForm.action = "user.php";

     var name= document.createElement("input");
      name.type = "hidden";
      name.id = "name";
      name.value = data.name;
      mapForm.appendChild(name);

      var loc= document.createElement("input");
      loc.type = "hidden";
      loc.id = "loc";
      loc.value = data.loc;
      mapForm.appendChild(loc);
      document.body.appendChild(mapForm );

      map=window.open("", "Map", "height=500,width=800,scrollbars=yes, resizable=yes");
     if (map) {
       mapForm.submit();
     } else {
       alert('You must allow popups for this map to work.');
      }



} );

【问题讨论】:

标签: javascript button click window.open


【解决方案1】:

使用这个示例代码.....

将此代码放在页面的头部,

<script>
    var name = "";
    var loc = "";
    collectData()
    {
        document.getElementById("name").value = name;
        document.getElementById("loc").value = loc;
    }
</script>

将此代码放在您想要按钮的表格中,

<form action="user.php" method="POST" onsubmit="collectData()">
    <input type="hidden" id="name">
    <input type="hidden" id="loc">
    <input type=submit value="Button">
</form>

为您的 javascript 代码中的变量赋值,

name = data.name;
loc = data.loc;

并使用此代码检索 user.php 中的数据,

$name = $_POST["name"];
$loc = $_POST["loc"];

您可以通过这种方式传递任意数量的值... 祝你好运!

【讨论】:

  • 我尝试构建一个表单并将其提交给其他 php ,但 user.php 什么也没得到,我已经更新了我的问题。我的代码有什么问题?
  • 您没有在表单中添加按钮 :O
  • 对不起,上面的评论是我的错误。我想你必须看看“data.name”和“data.loc”中是否有数据。 @user6556472
【解决方案2】:

请改用$_POST。 Post 不显示 url 中的变量。只需重定向到您的 php 文件,当您被重定向时,使用以下命令获取变量:

$variable = $_POST['input_name'];

【讨论】:

    【解决方案3】:

    根据问题Window.Open POSTEvert给出的答案

    您不能触发 javascript 弹出窗口然后强制发布请求。

    三个选项:

    1. 使用 javascript 触发带有 target="_blank" 的 POST 表单(但这不允许您禁用界面元素,例如菜单栏)。
    2. 在本地打开一个弹出窗口,但不要指定 url。使用 window.open 的结果来更改文档以生成一个表单,然后您将其发布。

      var myWindow = window.open("", "", "height=600,width=800,scrollbars=1,location=no,menubar=no,resizable=1,status=no,toolbar=no");
      myWindow.document.write("Write a form here and then later on trigger it");
      
    3. 你真的不应该这样做。如果用户复制 url 对用户不利,则说明您的应用程序设计存在缺陷。

    4. 编辑后添加:使用“空窗口”方法,但不是编写表单并触发它,而是在父级中执行 XMLHTTPRequest(带有 POST)。此请求的结果可用于填充子窗口。

    【讨论】:

    • 我尝试构建一个表单并将其提交给其他 php ,但 user.php 什么也没得到,我已经更新了我的问题。我的代码有什么问题?
    猜你喜欢
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多