Embed PHP in Javascript

447
June 23, 2017, at 12:02 PM

I need to embed PHP into Javascript so that when the user selects Countries it would display the result from the query alphabatically and if he selects Numbers then list based on numbers in descending.

Having researched, I have applied this (echo concept into my code) but doesn't seem to work.

I have the following query written in PHP that output staffs' country of birth( no of staff born in number of countries) in ascending order:

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

Then, I have a dropdown in 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>

Furthermore, I have a Javascript function 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?>;
    }
}

First I am going only for Numbersbecause the same logic will apply to the Countries. Any help will be appreciated

Rent Charter Buses Company
READ ALSO
How to use jQuery to switch divs on and off

How to use jQuery to switch divs on and off

Okay so I am looking at this website, http://codetunnelnet/, and I saw that it only used one page, but jQuery was used to display different divs, therefore making it feel like a multipage website

314
Synchronize scrollbars between two elements - jQuery

Synchronize scrollbars between two elements - jQuery

How can you synchronize scrollbars between two elements without recursively calling each event?

410
Issue with ngDialog - Angularjs

Issue with ngDialog - Angularjs

I have installed in my project the ngDialog module using bower install ng-dialogI can verify that this is installed by checking my bower

390
Uncaught TypeError: in jQuery

Uncaught TypeError: in jQuery

I am using confirm dialog of jquery-confirmjs plugin in my project

524