0

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

Zane
  • 33
  • 1
  • 8
  • 2
    PHP is server side scripts. JavaScript is client side scripts. You can't embed PHP into JavaScript. Are you trying to use PHP to print some JavaScript code? – ajreal Jun 23 '17 at 05:55
  • You must use ajax – Saba Tandashvili Jun 23 '17 at 05:57
  • I gathered that from the linked answer in my question. No, I am trying to display the query's(written in PHP) result in my Javascript function somehow – Zane Jun 23 '17 at 05:58
  • 1
    This is not possible. Client asks for the page; then server executes the PHP and outputs the response; then client draws the HTML/CSS and executes the JavaScript. You can't "climb back in time" to execute PHP again once your web page is on the client. You must use another request, tell the server to send some more data your way, then render it. – Amadan Jun 23 '17 at 05:58
  • So, use Ajax or connect to the database over again (which can be a nifty process from performance perspective) and was avoiding that – Zane Jun 23 '17 at 06:02
  • Possible duplicate of [How to embed php in javascript?](https://stackoverflow.com/questions/3352576/how-to-embed-php-in-javascript) – O.Rares Jun 23 '17 at 06:09

1 Answers1

0

So, i finally did it! Used switch instead of IF statement. Below is the code:

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

Hope it helps someone

Zane
  • 33
  • 1
  • 8