亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 PHP 代碼添加到動態生成的 HTML 數據中

將 PHP 代碼添加到動態生成的 HTML 數據中

茅侃侃 2023-10-24 20:02:24
我正在嘗試創建一個網站,該網站基本上有很多按鈕,這些按鈕將在單擊時執行特定的 PHP 文件,并在同一屏幕上的空框中顯示其結果。這些 PHP 文件位于test_scripts與index.php.我認為顯而易見的步驟是創建一個循環來迭代這些文件并動態創建它們的用戶界面。所以我在中間寫了下面的PHP代碼index.php#Index.php... <?php    $dir_itr = new DirectoryIterator("test_scripts");    foreach ($dir_itr as $file) {        if ($file->isFile()) {            $filename = $file->getFilename();            $formattedFileName =                '<button                     class="box button is-large is-fullwidth is-primary is-light"                     onclick="loadScriptFileData($filename)">                               $filename                 </button>';            print $formattedFileName;        }    }?>...這loadScriptFileData()是一個用 head 中的 script 標簽編寫的 JavaScript 函數:<!--index.php--><script>    function loadScriptFileData(filename) {        alert(filename);    }</script>foreach循環運行正常,但 JavaScript 函數和生成的 HTML 無法正常工作。頁面上有預期數量的按鈕,但每個按鈕只有名稱作為單詞"$filename",而不是實際預期的文件名。JavaScript 的情況更糟,它直接不起作用并在控制臺中給出錯誤,如下所示Uncaught ReferenceError: $filename is not defined at HTMLButtonElement.onclick ((index):71)為什么 $variable 沒有轉換為字符串?我什至嘗試了 toString() 函數,但仍然不好。我做錯了嗎?
查看完整描述

3 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

使用單引號時不會解析/解釋變量。

代替

$formattedFileName= '<button 
  class="box button is-large is-fullwidth is-primary is-light" 
  onclick="loadScriptFileData($filename)">$filename</button>';

$formattedFileName= "<button 
  class=\"box button is-large is-fullwidth is-primary is-light\" 
  onclick=\"loadScriptFileData('$filename')\">$filename</button>";


查看完整回答
反對 回復 2023-10-24
?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

另一種選擇(這是我個人的偏好)是在輸出 HTML 時結束 PHP 塊,并在需要時回顯 PHP 變量:


if ($file->isFile()) {

    $filename = $file->getFilename();

    // Let's end the PHP block

    ?>


        <button class="box button is-large is-fullwidth is-primary is-light"

            onclick="loadScriptFileData('<?= $filename ?>')">

            <?= $filename ?>

        </button>


    <?php // Open the PHP block again

}

這樣做的好處是 IDE 會在語法上正確突出顯示代碼(大多數 IDE 不會對 PHP 中的 HTML 內引號執行此操作)。


您也不需要轉義引號或手動打印內容,因為它會立即輸出。


查看完整回答
反對 回復 2023-10-24
?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

這是個人喜好,但我建議使用串聯或花括號:


使用大括號(注意:必須用雙引號括起來):


$formattedFileName =

    "<button

        class="box button is-large is-fullwidth is-primary is-light"

        onclick="loadScriptFileData({$filename})">

        {$filename}

    </button>";

使用串聯:


$formattedFileName =

    '<button

        class="box button is-large is-fullwidth is-primary is-light"

        onclick="loadScriptFileData(' . $filename . ')">

        ' . $filename . '

    </button>';

是的,如果使用雙引號,則可以在 PHP 的字符串中使用變量,而且肯定有很多人喜歡這樣做。對我來說,使用這兩種方法之一似乎更干凈且不易出錯。


使用雙引號會導致您對 HTML 屬性使用單引號或強制您轉義它們。


如果您不希望變量后面有空格,則不使用連接或花括號可能會導致問題:


$var = 'Pizza';

echo "$vars are awesome!" // Not the best example, but you get the idea

歸根結底,這是個人喜好。但正如另一個答案中所述,如果用單引號括起來,則不能在字符串中使用變量。


查看完整回答
反對 回復 2023-10-24
  • 3 回答
  • 0 關注
  • 153 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號