繁体   English   中英

如何将搜索值从一个php文件传递到另一个

[英]How to pass searched value from one php file to another

我正在尝试将searchschoolsthis.php中的搜索值传递给excel.php。 当我单击“ Cetak”时,它将生成excel文件,但没有搜索到的数据。 我试着把这个检查excel.php:

$strSQL = "SELECT * FROM schools WHERE kod_sekolah='Q'";

它打印我想要的数据,它是任何包含“ Q”的数据kod_sekolah。 这意味着excel.php正在运行。 但是,如何通过单击“ Cetak”将在searchschools.php中输入并搜索的数据传递给excel.php?

searchschoolsthis.php

 <body>



<form method="post" action="searchschoolsthis.php">
<input type="hidden" name="submitted" value="true" />

<label><span class="style2">Pilih Kategori:</span>
<span class="style1">
<select name="category">
  <option value="negeri">NEGERI</option>
  <option value="daerah">DAERAH</option>
  <option value="kod_sekolah">KOD SEKOLAH</option>
</select>
 </span></label>

 <span class="style1">
 <span class="style2">Taip Kriteria
 <label>:</label>
 </span>
 <label>
 <input type="text" name="criteria" onKeyUp="this.value = this.value.toUpperCase();"/>
 </label>
 <input type="submit" value="Cari" />
            </form>  


                            <form action="excel.php" method="post" name="criteria" target="_blank" id="cetak">
            <input type="submit" name="cetak" id="cetak" value="Cetak" />

             </form>


 </span>


   <table width='100%' color='black' id='header'>

   <thead>
    <tr>
      <td width="40%" scope="col"><div align="center"><span class="style2">Kod Sekolah</span></div></td>
      <td width="40%" scope="col"><div align="center"><span class="style2">Nama Sekolah</span></div></td>
      <td width="40%" scope="col"><div align="center"><span class="style2">PTJ</span></div></td>
      <td width="40%" scope="col"><div align="center"><span class="style2">Server</span></div></td>
      <td width="40%" scope="col"><div align="center"><span class="style2">PC</span></div></td>
      <td width="40%" scope="col"><div align="center"><span class="style2">NB</span></div></td>
      <td width="40%" scope="col"><div align="center"><span class="style2">Mono Laser</span></div></td>
      <td width="40%" scope="col"><div align="center"><span class="style2">Color Laser</span></div></td>
      <td width="40%" scope="col"><div align="center"><span class="style2">Dot Matrix</span></div></td>
      <td width="40%" scope="col"><div align="center"><span class="style2">LCD</span></div></td>
      <td width="40%" scope="col"><div align="center"><span class="style2">Set LAN</span></div></td>
      <td width="40%" scope="col"><div align="center"><span class="style2">Jumlah Kos</span></div></td>
      <td width="40%" scope="col"><div align="center"><span class="style2">Dibayar</span></div></td>
      <td width="50%" scope="col"><div align="center"><span class="style2">Tanggungan</span></div></td>
    </tr>
  </thead>
    </table>


<?php

    if(isset($_POST['submitted'])){

    include('connect.php');
    $category=$_POST['category'];
    $criteria=$_POST['criteria'];
    $query="SELECT * FROM schools WHERE $category = '$criteria'";
    $result=mysqli_query($dbcon,$query) or die('error getting data');


    echo "<table width='120%' border='2' color='black' id='header'>";


    while ($row = mysqli_fetch_array($result,MYSQL_ASSOC)){


    echo"<tr><td>";

    echo $row['kod_sekolah'];

    echo"</td><td>";       

    echo $row['nama_sekolah'];
    echo"</td><td>";

    echo $row['ptj'];
    echo"</td><td>";

    echo $row['server'];
    echo"</td><td>";

    echo $row['pc'];
    echo"</td><td>";

    echo $row['nb'];
    echo"</td><td>";

    echo $row['mono_laser'];
    echo"</td><td>";

    echo $row['color_laser'];
    echo"</td><td>";

    echo $row['dot_matrix'];
    echo"</td><td>";

    echo $row['lcd'];
    echo"</td><td>";

    echo $row['set_lan'];
    echo"</td><td>";

    echo $row['jumlah_kos'];
    echo"</td><td>";

    echo $row['dibayar'];
    echo"</td><td>";

    echo $row['tanggungan'];
    //echo"</td><td  style='text-align:right'>";

    echo"</td></tr>";

    }

    echo "</table>";

    }


    ?>
    </body> 

excel.php

    <body>
<?


   include("connect.php");
            $objDB = mysqli_select_db($dbcon,$strSQL);

            $strSQL = "SELECT * FROM schools WHERE kod_sekolah='Q'";
            //$strSQL = "SELECT * FROM schools WHERE negeri== '$criteria'";
            $objQuery = mysqli_query($dbcon,$strSQL);
            if($objQuery)
            {                      

                    $strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"])));


                    $strFileName = "Excel.xls";


                    $xlApp = new COM("Excel.Application");
                    $xlBook = $xlApp->Workbooks->Add();



                    $xlBook->Worksheets(1)->Name = "Rekod";                                                
                    $xlBook->Worksheets(1)->Select;


                    $xlApp->ActiveSheet->Range("A1:A1")->ColumnWidth = 10.0;
                    $xlApp->ActiveSheet->Range("B1:B1")->ColumnWidth = 13.0;
                    $xlApp->ActiveSheet->Range("C1:C1")->ColumnWidth = 23.0;
                    $xlApp->ActiveSheet->Range("D1:D1")->ColumnWidth = 12.0;
                    $xlApp->ActiveSheet->Range("E1:E1")->ColumnWidth = 13.0;
                    $xlApp->ActiveSheet->Range("F1:F1")->ColumnWidth = 12.0;


                    $xlApp->ActiveSheet->Range("A1:F1")->BORDERS->Weight = 1;
                    $xlApp->ActiveSheet->Range("A1:F1")->MergeCells = True;
                    $xlApp->ActiveSheet->Range("A1:F1")->Font->Bold = True;
                    $xlApp->ActiveSheet->Range("A1:F1")->Font->Size = 20;
                    $xlApp->ActiveSheet->Range("A1:F1")->HorizontalAlignment = -4108;                              
                    $xlApp->ActiveSheet->Cells(1,1)->Value = "Rekod Penyelenggaraan ICT di Sekolah-Sekolah";


                    $xlApp->ActiveSheet->Cells(3,1)->Value = "Kod Sekolah";
                    $xlApp->ActiveSheet->Cells(3,1)->Font->Bold = True;
                    $xlApp->ActiveSheet->Cells(3,1)->VerticalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,1)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,1)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells(3,2)->Value = "Nama Sekolah";
                    $xlApp->ActiveSheet->Cells(3,2)->Font->Bold = True;
                    $xlApp->ActiveSheet->Cells(3,2)->VerticalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,2)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,2)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells(3,3)->Value = "PTJ";
                    $xlApp->ActiveSheet->Cells(3,3)->Font->Bold = True;
                    $xlApp->ActiveSheet->Cells(3,3)->VerticalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,3)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,3)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells(3,4)->Value = "Server";
                    $xlApp->ActiveSheet->Cells(3,4)->Font->Bold = True;
                    $xlApp->ActiveSheet->Cells(3,4)->VerticalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,4)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,4)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells(3,5)->Value = "PC";
                    $xlApp->ActiveSheet->Cells(3,5)->Font->Bold = True;
                    $xlApp->ActiveSheet->Cells(3,5)->VerticalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,5)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,5)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells(3,6)->Value = "NB";
                    $xlApp->ActiveSheet->Cells(3,6)->Font->Bold = True;
                    $xlApp->ActiveSheet->Cells(3,6)->VerticalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,6)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,6)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells(3,7)->Value = "Mono Laser";
                    $xlApp->ActiveSheet->Cells(3,7)->Font->Bold = True;
                    $xlApp->ActiveSheet->Cells(3,7)->VerticalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,7)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,7)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells(3,8)->Value = "Color Laser";
                    $xlApp->ActiveSheet->Cells(3,8)->Font->Bold = True;
                    $xlApp->ActiveSheet->Cells(3,8)->VerticalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,8)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,8)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells(3,9)->Value = "Dot Matrix";
                    $xlApp->ActiveSheet->Cells(3,9)->Font->Bold = True;
                    $xlApp->ActiveSheet->Cells(3,9)->VerticalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,9)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,9)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells(3,10)->Value = "LCD";
                    $xlApp->ActiveSheet->Cells(3,10)->Font->Bold = True;
                    $xlApp->ActiveSheet->Cells(3,10)->VerticalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,10)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,10)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells(3,11)->Value = "Set LAN";
                    $xlApp->ActiveSheet->Cells(3,11)->Font->Bold = True;
                    $xlApp->ActiveSheet->Cells(3,11)->VerticalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,11)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,11)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells(3,12)->Value = "Jumlah Kos";
                    $xlApp->ActiveSheet->Cells(3,12)->Font->Bold = True;
                    $xlApp->ActiveSheet->Cells(3,12)->VerticalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,12)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,12)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells(3,13)->Value = "Dibayar";
                    $xlApp->ActiveSheet->Cells(3,13)->Font->Bold = True;
                    $xlApp->ActiveSheet->Cells(3,13)->VerticalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,13)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,13)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells(3,14)->Value = "Tanggungan";
                    $xlApp->ActiveSheet->Cells(3,14)->Font->Bold = True;
                    $xlApp->ActiveSheet->Cells(3,14)->VerticalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,14)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells(3,14)->BORDERS->Weight = 1;



                    $intRows = 4;
                    while($objResult = mysqli_fetch_array($objQuery))
                    {


                    $xlApp->ActiveSheet->Cells($intRows,1)->Value = $objResult["kod_sekolah"];
                    $xlApp->ActiveSheet->Cells($intRows,1)->BORDERS->Weight = 1;
                    $xlApp->ActiveSheet->Cells($intRows,1)->HorizontalAlignment = -4108;

                    $xlApp->ActiveSheet->Cells($intRows,2)->Value = $objResult["nama_sekolah"];
                    $xlApp->ActiveSheet->Cells($intRows,2)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells($intRows,3)->Value = $objResult["ptj"];
                    $xlApp->ActiveSheet->Cells($intRows,3)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells($intRows,4)->Value = $objResult["server"];
                    $xlApp->ActiveSheet->Cells($intRows,4)->HorizontalAlignment = -4108;
                    $xlApp->ActiveSheet->Cells($intRows,4)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells($intRows,5)->Value = $objResult["pc"];
                    $xlApp->ActiveSheet->Cells($intRows,5)->BORDERS->Weight = 1;
                    $xlApp->ActiveSheet->Cells($intRows,5)->HorizontalAlignment = -4108;

                    $xlApp->ActiveSheet->Cells($intRows,6)->Value = $objResult["nb"];
                    $xlApp->ActiveSheet->Cells($intRows,6)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells($intRows,7)->Value = $objResult["mono_laser"];
                    $xlApp->ActiveSheet->Cells($intRows,7)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells($intRows,8)->Value = $objResult["color_laser"];
                    $xlApp->ActiveSheet->Cells($intRows,8)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells($intRows,9)->Value = $objResult["dot_matrix"];
                    $xlApp->ActiveSheet->Cells($intRows,9)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells($intRows,10)->Value = $objResult["lcd"];
                    $xlApp->ActiveSheet->Cells($intRows,10)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells($intRows,11)->Value = $objResult["set_lan"];
                    $xlApp->ActiveSheet->Cells($intRows,11)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells($intRows,12)->Value = $objResult["jumlah_kos"];
                    $xlApp->ActiveSheet->Cells($intRows,12)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells($intRows,13)->Value = $objResult["dibayar"];
                    $xlApp->ActiveSheet->Cells($intRows,13)->BORDERS->Weight = 1;

                    $xlApp->ActiveSheet->Cells($intRows,14)->Value = $objResult["tanggungan"];
                    $xlApp->ActiveSheet->Cells($intRows,14)->BORDERS->Weight = 1;

                    $intRows++;
                    }                              

                    @unlink($strFileName);  

                    $xlBook->SaveAs($strPath."/".$strFileName);



                    $xlApp->Application->Quit();
                    $xlApp = null;
                    $xlBook = null;
                    $xlSheet1 = null;

            }



?>
Fail Excel Telah Berjaya Dijana. <a href="<?=$strFileName?>">Klik Disini</a> Untuk Muat Turun.
</body>

我会说就是改变

<form action="excel.php" method="post" name="criteria" target="_blank" id="cetak">
   <input type="submit" name="cetak" id="cetak" value="Cetak" />
</form>

<form action="excel.php?criteria=<?php echo url_encode($_POST['criteria']); ?>" method="post" name="criteria" target="_blank" id="cetak">
   <input type="submit" name="cetak" id="cetak" value="Cetak" />
</form>

并更改:

$strSQL = "SELECT * FROM schools WHERE kod_sekolah='Q'";

至:

$strSQL = "SELECT * FROM schools WHERE kod_sekolah='" . $_GET['criteria'] . "'";

PS:这只是基本代码,您需要保护它免受SQL注入等攻击。PPS:有多种方法可以使这只猫皮肤化。 这只是一种方式。

尝试类似:

<form action="excel.php" method="post" name="criteria" target="_blank" id="cetak">
    <input type="submit" name="cetak" id="cetak" value="Cetak" />
    <input type="hidden" name="criteria" value="<?= isset($_POST['criteria']) ? $_POST['criteria'] : '' ?>" />
</form>

暂无
暂无

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

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