2017-06-23 64 views
0

我需要將PHP嵌入到Javascript中,這樣當用戶選擇Countries時,它會以字母順序顯示查詢結果,如果他選擇Numbers,則會根據數字以降序列出。使用Javascript嵌入PHP

經過研究,我已經將thisecho概念加入我的代碼中),但似乎不起作用。

我有以下查詢用PHP編寫的該輸出員工的出生國(沒有出生在國家的一些工作人員)按升序排列:

$querytest = "select x , COUNT(*) from(select `staffbirthplace` as x from staffbirthdetails where staffemailid IN(SELECT staffemailid FROM staff where orgid='" . $orgId . "' AND deptname='" . $deptName . "' AND teamname='" . $teamName . "')) as temptable group by x order by count(*) ASC "; 

然後,我有一個下拉菜單中HTML

<form> 
    <label for="sortorder">Sort by:</label> 
    <select id="sortByDropdown" onchange="sortBy(this);"> 
     <option value="alphabatically">Countries</option> 
     <option value="numbers">Number</option> 
    </select> 
</form> 

另外,我有一個Javascript功能sortBy()

function sortBy(){ 
    var sortByDrpdownDiv = document.getElementById('sortByDropdown'); 

if (sortByDrpdownDiv[sortByDrpdownDiv.selectedIndex].value == 'numbers'){ 
    alert("yo in if statement"); 
    <?php $querytest = "select x , COUNT(*) from(select `staffbirthplace` as x from staffbirthdetails where staffemailid IN(SELECT staffemailid FROM staff where orgid='" . $orgId . "' AND deptname='" . $deptName . "' AND teamname='" . $teamName . "')) as temptable group by x order by count(*) DESC "; 
    $result = mysql_query($querytest); 

    while ($row = mysql_fetch_assoc($result)) { 
      echo "<b>"; 
      echo $row['x']; 
      echo ": </b>&nbsp;"; 
      echo $row['COUNT(*)']; 
      echo "<br/>"; 
     }?> 
    document.getElementById('staffbirthplaces').innerHTML = <?php echo $row?>; 
    } 
} 

首先,我只針對Numbers,因爲相同的邏輯將適用於Countries。任何幫助將不勝感激

+2

PHP是服務器端腳本。 JavaScript是客戶端腳本。您不能將PHP嵌入到JavaScript中。您是否嘗試使用PHP來打印一些JavaScript代碼? – ajreal

+0

您必須使用ajax –

+0

我從我的問題的鏈接答案中收集。不,我試圖以某種方式在我的Javascript函數中顯示查詢的(用PHP編寫的)結果 – Zane

回答

0

所以,我終於做到了!使用switch而不是IF statement。下面是代碼:

function sortByAlphabetsOrNumbers(obj){ 

    var selectedValue = obj.options[obj.selectedIndex].value 
switch(selectedValue) 
{ 
    case "numberOfStaff": 
     document.getElementById('sortBy').innerHTML = 
     "<?php 
      include 'connection.php'; 

      $staffNumbersDesc = "select x , COUNT(*) from(select `staffbirthplace` as x from staffbirthdetails where staffemailid IN(SELECT staffemailid FROM staff where orgid='" . $orgId . "' AND deptname='" . $deptName . "' AND teamname='" . $teamName . "')) as temptable group by x order by count(*) DESC"; 
      $result = mysql_query($staffNumbersDesc);    
      while ($row = mysql_fetch_assoc($result)) 
      { 
       echo "<b>"; 
       echo $row['x']; 
       echo ": </b>&nbsp;"; 
       echo $row['COUNT(*)']; 
       echo "<br/>"; 
       } 
     ?>"; 
     document.getElementById('birthCountriesAlphabaticalOrder').style.display = "none"; 
     break; 

    case "countries": 
     document.getElementById('sortBy').innerHTML = 
     "<?php 
      include 'connection.php'; 

      $alphabaticalOrder = "select x , COUNT(*) from(select `staffbirthplace` as x from staffbirthdetails where staffemailid IN(SELECT staffemailid FROM staff where orgid='" . $orgId . "' AND deptname='" . $deptName . "' AND teamname='" . $teamName . "')) as temptable group by x"; 
      $result = mysql_query($alphabaticalOrder);    
      while ($row = mysql_fetch_assoc($result)) 
      { 
       echo "<b>"; 
       echo $row['x']; 
       echo ": </b>&nbsp;"; 
       echo $row['COUNT(*)']; 
       echo "<br/>"; 
       } 
     ?>"; 
     document.getElementById('birthCountriesAlphabaticalOrder').style.display = "none"; 
     break; 
} 
}; 

希望它可以幫助別人