php - 发布表单后重置隐藏变量

标签 php javascript html

我想下载搜索条件的输出记录到excel。 由于搜索条件的字段超过 20 个。我使用 POST 方法而不是 GET 提交表单。

单击“Download2excel”时,隐藏变量 name1 设置为"is"。所以我会检查我的代码中的条件。下载成功后,我想重置变量。

这是我的代码的示例结构。

    <html>
    <head>
        <?php
        if(trim($_POST['name1']) == 'yes') {
            header("Content-type: application/vnd-ms-excel");
            header("Content-Disposition: attachment; filename=$filename.xls");
        }else {
            ?>
    </head>
    <body onload="tempFn()">

        <script language="javascript" type="text/javascript">
            function tempFn(){  
                global_search.name1.value = "";
            }
            function gs_download2xl_c() {
                document.getElementById('name1').value="yes";
                document.global_search.submit();
            }
        </script>
        <form name="global_search" id="global_search" action="test_1.php" method="POST">
            <input type="text" id="name1" name="name1" />
            <a  class="underline"  style="cursor: pointer" onclick="gs_download2xl_c();">
                Download2excel
            </a>
        </form>
            <?php } ?>
    </body>
</html>

由于我在发布表单后没有执行表单,因此我无法访问其中的变量 name1。我想重置变量 name1。

请建议我一个更好的程序。

编辑:-

我想在提交表单后触发下载。由于下载到 Excel 的记录应遵循输入的搜索条件。

最佳答案

我似乎不太明白你的问题。但我肯定会建议一些事情:

  • header(...)应在任何输出发送到浏览器之前出现。这意味着您必须将该行移到任何其他输出行之前。检查http://php.net/manual/en/function.header.php
  • 尝试如下操作:

    <script>
    function tempFn(){
        global_search.name1.value = "";
    }
    </script>
    <body onload="tempFn()">
    </body>
    

编辑1:如果我理解您的要求,您会想要下载一个 Excel 文件,该文件的内容取决于对数据库或其他内容的查询。如果我错了请纠正我。为此,您可能需要了解如何将数据写入 Excel 格式。只需通过 header(...) 设置正确的 header 后简单地回显数据即可。为此,您可能需要通过 google 搜索 PHP 的输出 Excel。我用谷歌搜索,得到了这个:http://code.google.com/p/php-excel/

if(trim($_POST['name1']) == 'yes') {
    header("Content-type: application/vnd-ms-excel");
    header("Content-Disposition: attachment; filename=$filename.xls");
    //echo the data here. 
}
else {
    //the form goes here
}

编辑2 不!标签“外部”的每个字符都会“输出”到浏览器。在你的情况下<html><head>将转到浏览器。然后您在 PHP 脚本中设置内容类型,这是不正确的。 试试这个:

 <?php
    if(trim($_POST['name1']) == 'yes') {
        header("Content-type: application/vnd-ms-excel");
        header("Content-Disposition: attachment; filename=$filename.xls");
    }else {
        ?>
<html>
<head>
    <script language="javascript" type="text/javascript">
        function tempFn(){  
            global_search.name1.value = "";
        }
        function gs_download2xl_c() {
            document.getElementById('name1').value="yes";
            document.global_search.submit();
        }
    </script>
</head>
<body onload="tempFn()">
    <form name="global_search" id="global_search" action="test_1.php" method="POST">
        <input type="text" id="name1" name="name1" />
        <a  class="underline"  style="cursor: pointer" onclick="gs_download2xl_c();">
            Download2excel
        </a>
    </form>
        <?php } ?>
</body>
</html>

关于php - 发布表单后重置隐藏变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4585599/

相关文章:

php mysql 在连接表上搜索部分匹配的数据

javascript - 如何在单个变量下为下拉列表设置多个值?

javascript - 对 2 个数组中的每个值求和

python - Python 中的代码,在 Node.js 和 Socket.IO 中通信,在 HTML 中呈现

html - Jquery Click 事件在第一次点击时不触发,但在第二次点击时触发,为什么?

php - 获取远程数据 - PHP 还是 JavaScript?

PHP 加载 MySQL 时出错

javascript - 我想使用 PHP 在圆形位置显示记录

javascript - document.getElementById 循环中

html - 禁用 Bootstrap 3 单页网站的事件链接样式