是否有可能在一個javascript函數中有php代碼?我有一個調用這個函數的按鈕,但它似乎不起作用。表格沒有顯示,沒有任何反應是否有可能在JavaScript函數中有php代碼?
<script>
function selectFunction()
{
<?php
$con = mysqli_connect("localhost","root","","study");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to mysql" . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM sample_employers");
while($row=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['middlename'] . "</td>";
echo "<td> <input type='button' value='Delete' </td>";
echo "</tr>";
}
mysqli_close($con);
?>
}
</script>
PHP代碼將執行第一,然後輸出將在您的JavaScript函數。它不會使用javascript調用php函數。 –
需要注意的是,PHP是服務器端語言,而Javascript是客戶端語言。 – aug
這被稱爲AJAX – zkanoca