如何从js获取变量并在php中使用它们

时间:2019-01-21 14:37:44

标签: javascript php excel

您好,我正在制作一个网站,您可以添加excel文件并选择一个人,当您选择一个人时,Web只会打印那些行,仅是您从选择框中选择的人。 有谁知道我怎么能从js函数(函数getSelectedPerson())获得结果变量,并在php中将resul插入语句中。

<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 个答案:

答案 0 :(得分:0)

我认为这就是您要寻找的。您不能在PHP中使用Javascript变量,而必须通过GET或POST请求传递值。您不需要示例的脚本部分。

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

更改

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

$chosenPerson = $_GET["dla_darbuotojo_pasirinkimas"];

您的PHP脚本中可能还需要一个条件

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>";
}

因此,如果没有人调用脚本,它将不会尝试运行该代码。