在Javascript中嵌入PHP

时间:2017-06-23 05:53:15

标签: javascript php html

我需要将PHP嵌入到Javascript中,以便当用户选择Countries时,它会以字母方式显示查询结果,如果他选择Numbers,则基于列表下降的数字。

经过研究,我已将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。任何帮助将不胜感激

1 个答案:

答案 0 :(得分: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;
}
};

希望它有助于某人