0

我有一个用户输入字段'from'和'to'必须提交到两个不同的php文件,它们将mysql表导出到csv文件并在新窗口中打开它以供下载。由于我有 2 个表和 2 个 CSV 文件要由用户下载,因此我使用此表单将值传递给 2 个执行导出的 php 文件。

下面的代码有 2 个表单和 2 个 php,只有第一个工作正常。第二种形式无法访问 $_POST 数组。

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>EXPORT INVOICE </title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <SCRIPT LANGUAGE="JavaScript">
      function runscript()
      {
        document.f1.submit();
        document.f2.submit();
      }
    </SCRIPT>

    </head>

    <body>

        <form name="f1" method="post" action="export-tst-with-header.php" target="_blank">

        <br>
        <span class="sty2">Enter to export From Invoice No.:</span> <input type="text" size='10' maxlength='10' name="finv">

        <span class="sty2">To Invoice No:</span>&nbsp; <input type="text"  size='10' maxlength='10' name="tinv">
        </form>

            <form name="f2" method="post" action="export-tst-with-header2.php" target="_blank">
            </form>

        <input type="button" value="Export" onClick="runscript()">


    </body>
    </html>

下面是第一个将 mysql 表转换为 csv 文件的 EXPORT 文件,它工作正常,第二个文件也一样,只是导出的表不同,这是不工作的:

    <?php

    //echo "Exporting file - process"."<br><br>";

    $dbservertype='mysql';
    $servername='localhost';
    $dbusername='aab';
    $dbpassword='aabs';
    $dbname='aab';

    ////////////////////////////////////////
    ////// DONOT EDIT BELOW  /////////
    ///////////////////////////////////////
    connecttodb($servername,$dbname,$dbusername,$dbpassword);
    function connecttodb($servername,$dbname,$dbuser,$dbpassword)
    {
    global $link;
    $link=mysql_connect ("$servername","$dbuser","$dbpassword");
    if(!$link){die("Could not connect to MySQL");}
    mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
    }
    //////// End of connecting to database ////////
    $from=$_POST['finv'];
    $to=$_POST['tinv'];

    //echo $_POST['action'];

    //if ($_POST['action'] == 'download')
    //{

        header("Content-type: application/csv");
        header("Content-Disposition: attachment; filename=downloadinv.csv");
        header("Pragma: no-cache");
        header("Expires: 0");



    $query = "SELECT * FROM INVHDR WHERE Invno between $from AND $to";

    $export = mysql_query ($query ) or die ( "Sql error : " . mysql_error( ) );

    $fields = mysql_num_fields ( $export );

    for ( $i = 0; $i < $fields; $i++ )
    {
        $header .= mysql_field_name( $export , $i ) . "\t";

    }

    while( $row = mysql_fetch_row( $export ) )
    {
        $line = '';
        foreach( $row as $value )
        {                                            
            if ( ( !isset( $value ) ) || ( $value == "" ) )
            {
                $value = "\t";
            }
            else
            {
                $value = str_replace( '"' , '""' , $value );
                $value = '"' . $value . '"' . "\t";
            }
            $line .= $value;
        }
        $data .= trim( $line ) . "\n";
    }
    $data = str_replace( "\r" , "" , $data );

    if ( $data == "" )
    {
        $data = "\n(0) Records Found!\n";                        
    }

    mysql_query("UPDATE INVHDR SET Export=1 WHERE Invno between $from AND $to");

    print "$header\n$data";


    //}
    exit();
    php?>
4

1 回答 1

0

您必须将 $_POST['finv'] 和 $_POST['tinv'] 传递到第二个 php 页面。要么将它们存储在 $_SESSION 变量中(并从第二页的 $_SESSION['finv'] 和 $_SESSION['tinv'] 中恢复它们,要么创建一个隐藏字段并重新发布它们。

于 2013-03-06T11:07:58.523 回答