簡體   English   中英

將PHP嵌入Javascript中

[英]Embed PHP in Javascript

我需要將PHP嵌入到Javascript以便當用戶選擇“ Countries ,它將按字母順序顯示查詢的結果,並且如果他選擇Numbers則根據降序列出數字。

經過研究,我已經將此方法 (將echo概念應用到我的代碼中)但似乎沒有用。

我有以下用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 任何幫助將不勝感激

所以,我終於做到了! 使用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;
}
};

希望對別人有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM