簡體   English   中英

PHP包含/需要方法帶有可變路徑

[英]PHP include/require method with variable path

有人幫我弄錯了! Ajax返回正確的信息。 它還在“成功”包含頁面上顯示,所有數據都在“ alert(html)”中。 回聲$ idName; 並回顯$ path; 在首頁上打印正確的信息(代碼在哪里)。 該頁面中沒有側面刷新。 Require方法不會阻止頁面。 問題是div include仍然為空! 謝謝。

<div id="include">
    <?php
        if (isset($_POST['idName1'])) {
            $idName = $_POST['idName1'];
            $query = mysqli_query($mysqli, "SELECT in_path FROM strani WHERE id_name='$idName '");
            $row_path = mysqli_fetch_array($query);
            $path = $row_path['in_path'];
            require($path); /*Also tried: require(dirname(__FILE__)."/".$path); all the same!*/
        }
    ?>
</div>

因為我想進一步解釋,所以不會像評論那樣通過,因此我聲明要編輯問題。 附加文件結構和目錄,以及更改為縮短過程的代碼,以顯示錯誤。 預測的情況如下:授權的助理將登錄,將打開main-page.php,其中包含允許編輯的表單的可選菜單。 此頁面立即包含“ form-carrier.php及其自身包含some-data.php”。 一旦通過定位標記,他將選擇適當的表格,該表格應以“ id = include”包含在適當的div中。

Page structure:
    main-page.php > It includes file: require(form-carrier.php) which iclude: require(form.php); and: require(some-data.php); 
Dir structure:
    secured-dir (It contains files: login.php; error.php; error-log)
        styles-dir
        js-dir
        includes-dir (It contains files: functions.php; db-conn-conf.php; process-login-logout.php; session-class.php; error-log)
        forms-dir (It contains files: form-carrier.php; some-data-php; error_log)
            forms-byclass1-subdir (It contains files: form-some-name.php; error_log)
            forms-byclass2-subdir (It contains files: form-some-name.php; error_log)
            ..... other sub dirs....
        working-dir It contains files: (main-page.php; error_log)

通過鍵入指定路徑進行測試,發現如果在require(“ forms-byclass1-subdir / form-some-name.php”)中進行刻寫,則該路徑可以正常工作; 以及“ ../forms-dir/forms-byclass1-subdir/form-some-name.php”。 這使我得出以下結論:“ form-some-name.php”由工作目錄中的main-page.php以及位於forms-dir中的form-carrier.php注冊。 mainpage.php的子級。 不應該這樣工作,但是可以工作。 為避免混淆,我在同一個目錄“ working-dir”和“ main-page.php”中替換了兩個包含文件(form-carrier.php和some-data-php)。

Now dir structure look like:
forms-dir (only error_log)
    forms-byclass1-subdir (It contains files: form-some-name.php; error_log)
    ..... others forms-byclass-subdirs....
working-dir It contains files: (main-page.php; form-carrier.php; some-data-php; error_log)

And the php code in form-carrier.php:
...
<div id="include">
    <?php
        if (isset($_POST['strPath1'])) {
            $strPath = $_POST['strPath1'];
            var_dump($strPath);
            require($strPath); 
        }
    ?>
</div>
...

我縮短了代碼,決定不遍歷數據庫,而是在javascript中使用完整路徑創建變量,然后通過AJAX將其直接發送到目標文件。 同樣,結果是相同的。 如果通過鍵入來刻寫路徑,則代碼將正確執行,如果我使用Ajax發送的javascript中的變量,則可以獲取正確的數據,alert(html)可以讀取表單名稱中的所有數據。 php以及form-carrier.php; 和some-data.php,var_dump($ strPath); 給我正確的路徑,但是include div仍然為空! 如果有人仍然有興趣為解決這個問題,我將不勝感激,這對我來說是未知而奇怪的問題。

由於沒有足夠的知識,有人想花時間解決這個問題,並且我相信這很容易解決,所以我暫時解決了,並希望能像我一樣對這個領域的其他初學者有用! 我回到第一種方法。 這種方法非常簡單,並提供了以下幾個步驟:page1或單擊> page1 js var以獲取路徑並分配給page1中的中間文件> js用filePath加載page2中的page3。 過早刷新的問題已修復如下:首先將JS文件拆分為三個文件的全部代碼,然后在每個頁面分別拆分三個文件,還將setTimeout函數添加到其他過程中以正確的順序執行。 現在運行順利!

main-page.php
<nav class=”side-menu”>
…
    <?php ... Creating a side-menu from database MySQL ... ?>
// echo resault:
<ul>
    <li class=”sub-menu”><a id=”forms-byclass1-subdir” href=”#”></a> 
        <ul>
            <li class=”anchor”>
                <a href=”# form-some-name”>
                    <span id=”form-header”>form-label</span></a>
</li></ul></li></ul></nav>
…
</nav>
…
<div id="fc">
    <?php require("form-carrier.php"); ?>
</div>
<div id=”hidden”
    <input type="text" id="path" name="path" value=”” />
</div>
…
form-carrier.php
…
<div id="include"></div> 
…
Main-page.js
…
$(".sub-menu > a").click(function(e){
    e.preventDefault();
    … variables to get a desired path …
    $("#include").load(path);
    setTimeout(function() {
        … other procedures …
    }, 60);
});

這並不意味着我已經放棄使用Ajax,PHP和db的方法,但是在找到解決方法之前,這當然是一個不錯的解決方案。 因此,仍然需要網頁設計大師的貢獻。

請注意各種事件的正常運行:由於該文件是動態包含的,因此使用JQuery .click()以及JavaScript HTML DOM EventListener(如果未在html內聯中使用)會出現問題。 至少就我而言,最可靠的方法是使用Jquery“ .on(events [,selector] [,data],handler)”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM