0

Hi all i was making a website were you can add excel file and choose a person and when you choose a person then it the web would only print those rows were only is a person that you chosen from select box. Does anyone know how i could get result variable from js function (function getSelectedPerson()) and that resul insert in php for statement.

<script>
   var filling_prices= new Array();
    filling_prices["None"]=0;
    filling_prices["dla_dei_mat"]="Deividas Matulis";
    filling_prices["dla_tom_ver"]="Tomas Veršinskas";
    filling_prices["dla_dar_ser"]="Darius Sereika";
    filling_prices["dla_dov_pra"]="Dovydas Prakapas";
     
  function getSelectedPerson()
  {
      var theForm = document.forms["dla_darbuotojo_pasirinkimas_form"];
     var selectedPerson = theForm.elements["dla_darbuotojo_pasirinkimas"];
     fillSelectedPerson=filling_prices[selectedPerson.value];

      return fillSelectedPerson;
  }

</script>
<?php
require_once "Classes/PHPExcel.php";
  $chosenPerson = $_GET["getSelectedPerson()"];
  $tmpfname = "visi.xls";
  $excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
  $excelObj = $excelReader->load($tmpfname);
  $worksheet = $excelObj->getSheet(0);
  $lastRow = $worksheet->getHighestRow();
  
  echo "<table>";
  for ($row = 1; $row <= $lastRow; $row++) {
   if ($chosenPerson ==  ($worksheet->getCell('D'.$row)->getValue()) ) {
    echo "<tr><td>";
    echo $worksheet->getCell('D'.$row)->getValue();
    echo "</td><td>";
    echo $worksheet->getCell('F'.$row)->getValue();
    echo "</td><td>";
    echo $worksheet->getCell('G'.$row)->getValue();
    echo "</td><tr>";
   }
    
  }
  echo "</table>";
?>

      <form name="dla_darbuotojo_pasirinkimas_form">
        <div class="Choose_people">
          <select name="dla_darbuotojo_pasirinkimas">
            <option value="None">Darbuotojai</option>
            <option value="dla_dei_mat">Deividas Matulis</option>
            <option value="dla_tom_ver">Tomas Versinskas</option>
            <option value="dla_dar_ser">Darius Sereika</option>
            <option value="dla_dov_pra">Dovydas Prakapas</option>
          </select>
        </div>
      </form>
  • 1
    Possible duplicate of [How to pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – Masivuye Cokile Jan 21 '19 at 14:40
  • You're going to need to point your form to your php script with action="xxx" , and then something like an onchange event on the select to post the form back to php with a GET or POST variable set – Matthew Page Jan 21 '19 at 14:42

1 Answers1

1

I think this is what you are looking for. You can not use Javascript variables in PHP, you have to pass the values via GET or POST requests. You don't need the script portion of your example.

<form name="dla_darbuotojo_pasirinkimas_form" action="yourscript.php" method="get">
    <div class="Choose_people">
      <select name="dla_darbuotojo_pasirinkimas" onchange="this.form.submit()>
        <option value="None">Darbuotojai</option>
        <option value="dla_dei_mat">Deividas Matulis</option>
        <option value="dla_tom_ver">Tomas Versinskas</option>
        <option value="dla_dar_ser">Darius Sereika</option>
        <option value="dla_dov_pra">Dovydas Prakapas</option>
      </select>
    </div>
  </form>

Change

$chosenPerson = $_GET["getSelectedPerson()"];

to

$chosenPerson = $_GET["dla_darbuotojo_pasirinkimas"];

You may also need a condition in your PHP script

if(isset($_GET["dla_darbuotojo_pasirinkimas"])) {
  // do the get person stuff

    $chosenPerson = $_GET["getSelectedPerson()"];
    $tmpfname = "visi.xls";
    $excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
    $excelObj = $excelReader->load($tmpfname);
    $worksheet = $excelObj->getSheet(0);
    $lastRow = $worksheet->getHighestRow();

    echo "<table>";
    for ($row = 1; $row <= $lastRow; $row++) {
        if ($chosenPerson ==  ($worksheet->getCell('D'.$row)->getValue()) ) {
            echo "<tr><td>";
            echo $worksheet->getCell('D'.$row)->getValue();
            echo "</td><td>";
            echo $worksheet->getCell('F'.$row)->getValue();
            echo "</td><td>";
            echo $worksheet->getCell('G'.$row)->getValue();
            echo "</td><tr>";
        }

    }
    echo "</table>";
}

So if the script has been called with no person it won't try to run that code.

Matthew Page
  • 746
  • 5
  • 15