[英]Using PHP to load Javascript
編輯:我的功能被破壞,其中包含/n
。 感謝woofmeow讓我質疑我的功能!
我是編程新手。 我一直在努力,過去幾個月我學到了很多東西(這里有一堆!)但我很難過。
我有一個PHP腳本從另一個PHP腳本調用,我無法獲得實際運行的代碼。 它曾經運行,然后我改變了一些東西,並沒有保存更改,我不知道我做了什么。 我知道,我知道,新手的錯誤(我從中學到了!)。 Javascript在視圖頁面源上顯示正常,但不再運行。
這是頁面源代碼,可能就是這么簡單:
<script type="text/javascript">
function delete_user(user_id)
{if (confirm("Are you sure you want to delete this user?" + "\nThere's really no going back!")) {window.location = "delete_user.php?user_id=" + user_id;}}</script>
這是PHP:show_users腳本將其發送到查看器:
$delete_user_script = <<<EOD
function delete_user(user_id)
{
if (confirm("Are you sure you want to delete this user?"
+ "\nThere's really no going back!"))
{
window.location = "delete_user.php?user_id=" + user_id;
}
}
EOD;
HTML中的PHP在工作時沒有改變:
<?php
while ($user = mysql_fetch_array($result))
{
$user_row = sprintf("<li><a href='show_user.php?user_id=%d'>%s %s</a>(<a href='mailto:%s'>%s</a>)<a href='javascript:delete_user(%d);'><img class='delete_user' src='../images/delete.png' width='15' /></a></li>",
$user['user_id'],
$user['first_name'],
$user['last_name'],
$user['email'],
$user['email'],
$user['user_id']);
echo $user_row;
}
?>
最后,查看腳本為我們提供了頁面源代碼(注意:$ embedded_javascript是$ delete_user_script):
if (!is_null($embedded_javascript))
{
echo '<script type="text/javascript">' . $embedded_javascript . '</script>';
}
當我將鼠標懸停在圖像上以刪除用戶時,它仍會顯示正確的腳本鏈接(“Javascript:delete_user(%d)”,其中%d是user_id)但是它就像沒有定義函數一樣,沒有任何反應。 任何想法都非常感謝! 謝謝!
基本上你的if
語句是錯誤的(即使在$delete_user_script
變量中)。 由於它是從一個新的線開始,口譯員將假設一個;
在它的最后,因此你的if
語句中斷。
你的功能有這個
if (confirm("Are you sure you want to delete this user?" + "\nThere's really no going back!"))
應該是這樣的
if (confirm("Are you sure you want to delete this user?" + "\nThere's really no going back!"))
有時它只是一個小小的錯誤。 希望有幫助:)
編輯1 :這是javascript和php代碼中的原始發布問題中的錯誤。 現在它已經過編輯,以反映if
是不是被打破了。 編輯2 :我被告知這有助於解決問題所以我會在這里保留這個問題。 希望它也能幫助別人。 PS :如果有人想刪除,請告訴我。
試試這個而不是你現在在腳本中的內容:
$delete_user_script = "".
"function delete_user(user_id)
{
if (confirm(\"Are you sure you want to delete this user?\" + \"\\nThere's really no going back!\"))
{
window.location = \"delete_user.php?user_id=\" + user_id;
}
}";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.