简体   繁体   中英

How to pass searched value from one php file to another

Im trying to pass a searched value which in searchschoolsthis.php to excel.php. When I click 'Cetak', it generate the excel file but without the searched data. I tried checking that excel.php by putting this:

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

It prints the data I wanted which is any data kod_sekolah that contains 'Q'. This means that the excel.php is working. But how can I pass the data which is entered and searched in searchschools.php to excel.php by clicking 'Cetak'?

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>

I would say that just change

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

to

<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>

and change:

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

to:

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

PS: This is just a basic code u need to protect it against SQL Injection etc. etc. PPS: There is more than one way to skin this cat. This is just one way.

Try something like:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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