繁体   English   中英

PHP将querystring传递到另一个页面

[英]PHP pass querystring to another page

我有三个文件。 主文件是dispatch.php。 从该文件中,用户将进行各种选择,这些选择将使用另一个页面上的查询构建器(称为dispatch-grid.php)。 在dispatch-grid.php中,查询被构建并显示为dispatch.php。

这是来自dispatch-grid.php的查询,该查询调用数据库并在dispatch.php上显示网格:

 <?php
   $select = "SELECT DISTINCT * FROM `dispatch_read`" . " WHERE " . $where . ";";
   $QueryResult = @mysql_query($select) or die ();
   $resnum = mysql_num_rows($QueryResult);

   if($resnum == 0){
      echo "<div>Your search returned no results</div>";
   }
   else {
     echo "<table>\n";
     echo "<thead><tr>" .
     echo "<th>BOL</th>" .
     echo "<th>CONTAINER</th>" .
     echo "<th>STATUS</th>" . 
     echo "</tr></thead>" .
     echo "<tbody>\n";

     while(($Row = mysql_fetch_assoc($QueryResult)) !== FALSE){
     echo "<tr>";
     echo "<td>{$Row[BOL]}</td>";
     echo "<td>{$Row[CONTAINER]}</td>";
     echo "<td>{$Row[STATUS]}</td>";
     echo "</tr></tbody>\n";
     echo "</table>\n";
     }
    }

我还要显示更多行。 我只是想让它尽可能短。

我可以使用以下功能在dispatch.php上显示此网格:

 displayrecords();

此时,网格显示在dispatch.php上。 我现在需要做的是将querystring从dispatch-grid.php($ select)传递到另一个名为getreport.php的页面,在这里我可以将网格导出到Excel工作表中。

在dispatch.php上,我有一个输入按钮:

 <input onclick="getreport()" type="button" value="Get Report" />

该按钮在同一页面上调用javascript函数:

 *** UPDATE ***
 function getreport(){
   window.location = "getreport.php?where=$where";
 }

在这一点上,我可以打开一个Excel工作表,但它打开空白。 我不确定是否需要显示,但是这是getreport.php上的代码:

 <?php
 include("include/database.php";

 global $header;
 global $data;
 global $ts;
 $ts = date('mdY-His');
 $sep = "\t";
 $filename = "excelfilename";

 *** UPDATE ***
 $sql = "SELECT DISTINCT * FROM dispatch_read WHERE " . $_GET['where'] . ";";

 $result = @mysql_query($sql) or die ("Couldn't execute query" . mysql_error());
 $file_ending = "xls";

 header("Content-Type: application/xls");
 header("Content-Disposition: attachment; filename=$filename.xls");
 header("Pragma: no-cache");
 header("Expires: 0");

 $sep = "\t";
 for ($i = 0; $i < mysql_num_fields($result); $i++) {
 echo mysql_field_name($result,$i) . "\t";
 }
 print("\n");
   while($row = mysql_fetch_row($result))
   {
     $schema_insert = "";
     for($j=0; $j<mysql_num_fields($result);$j++)
     {
       if(!isset($row[$j]))
         $schema_insert .= "NULL".$sep;
       elseif ($row[$j] != "")
         $schema_insert .= "$row[$j]".$sep;
       else
         $schema_insert .= "".$sep;
     }
   $schema_insert = str_replace($sep."$", "", $schema_insert);
   $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
   $schema_insert .= "\t";
   print(trim($schema_insert));
   print "\n";
 }
 ?>  

请忽略任何错别字。 我能够显示网格没有问题。 我的代码可以正常工作,直到他们单击getreport按钮。

我猜我需要做的是将查询($ select)从dispatch-grid.php发送到getreport.php,但是我不确定该怎么做。

我尝试在dispatch.php上执行此操作:

 $query_string = $_SERVER['QUERY_STRING'];

然后,我尝试将其发送到getreport.php,但发送失败。

我将不胜感激任何帮助。

谢谢。

您可以简单地更改此函数(getreport)以传递getreport.php所需的任何参数

function getreport(){
   window.location = "getreport.php?param1=hello&param2=world";
 }

因此,在getrport.php中,您只需访问以下参数,

echo $_GET['param1'];
echo $_GET['param2'];

更新资料

您要传递的是$ select =“ SELECT DISTINCT * FROM dispatch_read ”。 “哪里”。 $ where。 “;”;

我建议的是仅将$ where变量值传递给getreport.php。

function getreport(){
   window.location = "getreport.php?where=hello"; // where param should hold the value of $where 
 }

因此,在getreport.php中 ,您可以按以下方式访问它并执行相同的操作。

$ select =“选择DISTINCT * FROM dispatch_read ”。 “哪里”。 $ _GET ['where']。 “;”;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM