這就是我得到的。將PHP變量從按鈕傳遞到Javascript函數
<script type="text/javascript">
function show_confirm(id)
{
var r=confirm("Are you sure you want to duplicate this record?");
if (r==true)
{
window.open ("http://domain.com/process.php?duplicate=id","mywindow");
}
else
{
alert("Record Not Duplicated.");
}
}
</script>
則低於:
$query = "SELECT * FROM contacts";
$result = mysql_query($query);
while($row = mysql_fetch_row($result)){
$id = $row[0];
$company = $row[3];
$firstName = $row[4];
$protection = $row[20];
echo '<tr>';
echo '<td width="120" align="middle"><font color="black"><b>' . $company . '</b></font></td>';
echo '<td width="120" align="middle"><font color="black"><b>' . $firstName . '</b></font></td>';
echo "<td width='120' align='middle'><a style='text-decoration: none;' href='http://domain.com/index.php?id=14&edit=" . $id . "'><span style='font-family:Helvetica; color:black;'>Edit</span></a>";
echo "<td width='120' align='middle'><input type='button' onclick='show_confirm($id)' value='Duplicate' />";
echo "</td>";
echo '</tr>';
}
?>
問題是這樣的:
echo "<td width='120' align='middle'><input type='button' onclick='show_confirm($id)' value='Duplicate' />";
這應該是當用戶點擊它放在一個按鈕,它說:"are you sure you want to duplicate?"
那麼如果使用點擊是的,它到http://domain.com/process.php?duplicate=(whatever the ID is)
問題是我的變量(我n php)$id
,沒有被傳遞給javascript window.open
。我需要將行中的$id
傳遞給javascript。我試圖寫它,但它沒有奏效。甚至沒有看到變量。我嘗試過在<?php echo $id; ?>
中手動輸入,但這並不奏效。
謝謝