不能POST值给其他php



我有一个按钮在我的表,原来是使用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.');
      }

} );

根据问题窗口Evert给出的答案。打开帖子

你不能触发一个javascript弹出窗口,然后强制post请求。

三个选项:

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

    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)。此请求的结果可用于填充子窗口。

$_POST代替。Post不显示url中的变量。重定向到你的php文件,重定向后使用:

获取变量
$variable = $_POST['input_name'];

使用这个示例代码…

把这段代码放在你的页面标题部分,

<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"];

你可以通过这种方式传递任意数量的值…古德勒克!

最新更新