0
我需要將PHP
嵌入到Javascript
中,這樣當用戶選擇Countries
時,它會以字母順序顯示查詢結果,如果他選擇Numbers
,則會根據數字以降序列出。使用Javascript嵌入PHP
經過研究,我已經將this(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> ";
echo $row['COUNT(*)'];
echo "<br/>";
}?>
document.getElementById('staffbirthplaces').innerHTML = <?php echo $row?>;
}
}
首先,我只針對Numbers
,因爲相同的邏輯將適用於Countries
。任何幫助將不勝感激
PHP是服務器端腳本。 JavaScript是客戶端腳本。您不能將PHP嵌入到JavaScript中。您是否嘗試使用PHP來打印一些JavaScript代碼? – ajreal
您必須使用ajax –
我從我的問題的鏈接答案中收集。不,我試圖以某種方式在我的Javascript函數中顯示查詢的(用PHP編寫的)結果 – Zane