PHP包含/需要使用可变路径的方法 [英] PHP include/require method with variable path

查看:88
本文介绍了PHP包含/需要使用可变路径的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人帮助我错了! Ajax返回正确的信息。它还显示'成功'包括页面中的所有数据'alert(html)'。 Echo $ idName;和echo $ path;在载体页面上打印正确的信息(代码在哪里)。页面中没有边茶点。 Require方法不会阻止页面。问题是div包含空的!谢谢。

 < 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); / *还试过:require(dirname(__ FILE __)。/。$ path);都一样!* /
}
?>
< / div>

因为我想进一步解释,它不会像评论那样传递,我声明要编辑题。附上文件结构和目录,以及更改缩短过程的代码,以显示错误。预测的情景如下:授权的联系人自登录以后,将打开main-page.php,并带有可供编辑的可选表单菜单。这个页面立即包含'form-carrier.php,它自带include some data.php'。一旦通过锚标签,他将选择适当的表格,这个表格应该被包含在适当的div中,id = include。

 页面结构:
main-page.php>它包括file:require(form-carrier.php)其中iclude:require(form.php);和:require(some-data.php);
目录结构:
secured-dir(它包含文件:login.php; error.php; error-log)
styles-dir
js-dir
包含-dir(它包含文件:functions.php; db-conn-conf.php; process-login-logout.php; session-class.php;错误日志)
forms-dir(它包含文件:form (包含文件:form-some-name.php; error_log)
forms-byclass2-subdir(它包含文件:form-some-name.php; error_log)
.....其他子目录....
工作目录它包含文件:(main-page.php; error_log)

通过键入来指定路径进行测试,并发现如果在require中加入它,它会正常工作(forms-byclass1-子目录/形状一些-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)。

 现在dir结构如下所示:
forms-dir(only error_log)
forms-byclass1-subdir(它包含文件:form-some-name .php; error_log)
.....其他形式-byclass-subdirs ....
working-dir它包含文件:(main-page.php; form-carrier.php; some- data-php; error_log)

和form-carrier.php中的php代码:
...
< div id =include>
<?php
if(isset($ _ POST ['strPath1'])){
$ strPath = $ _POST ['strPath1'];
var_dump($ strPath);
require($ strPath);
}
?>
< / div>
...

我缩短了代码并决定不经过数据库,但在javascript中使用完整路径创建变量,并通过AJAX直接将其发送到目标文件。结果是一样的。如果通过键入来标记路径,那么代码会正确执行,如果我使用由Ajax发送的变量,我会得到正确的数据,在alert(html)中可以从form-some-name中读取所有数据。 PHP,以及form-carrier.php;和some-data.php,var_dump($ strPath);给我正确的路径,但包括div仍然是空的!我会非常感激,如果有人仍然有兴趣有助于解决这个问题,对我来说是未知和奇怪的问题。

由于没有人与足够的知识,想花时间解决这个问题,我相信这是很容易解决的,我现在解决了,并希望找到有用的其他初学者在这方面对我有用!我回到了第一种方法。这种方法简单得多,并提供了以下几个步骤:page1 achor click> page1 js var for path和asign to intermediate filed in page1> js使用filePath在page2中加载page3。过早刷新的问题修复如下:首先将JS文件拆分为三个文件的所有代码,分别为每个页面三个代码,并将setTimeout函数添加到其他程序中,以正确执行顺序执行。

  main-page.php 
< nav class =side-menu>
...
<?php ...从数据库MySQL创建一个侧边菜单...?>
// echo resault:
< ul>
< li class =sub-menu>< a id =forms-byclass1-subdirhref =#>< / 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 =textid =pathname =pathvalue =/>
< / div>
...
form-carrier.php
...
< div id =include>< / div>
...
Main-page.js
...
$(。sub-menu> a)。click(function(e){
e.preventDefault ();
...获得所需路径的变量...
$(#include)。load(path);
setTimeout(function(){
... other procedures ...
},60);
});

这并不意味着我放弃了使用Ajax,PHP和db的方法,但直到我找到了修复它的方法,这当然是一个很好的解决方案。因此,仍然需要网页设计专家的贡献。



正确运行各种事件的注意事项:因为该文件是动态包含的,将使用JQuery .click()以及JavaScript HTML DOM EventListener(如果它未在html内联中使用)会产生问题。至少在我的情况下,最可靠的方法是使用Jquery.on(events [,selector] [,data],handler)。

Someone to help where I'm wrong! Ajax returns correct information. It also shows on 'success' include page with all data in 'alert(html)'. Echo $idName; and echo $path; print correct information in carrier page (where is the code). There is no side refreshments in the page. Require method does not block the page. The problem is that div include remains empty! Thanks.

<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>

Because what I want to explain further, it will not pass like a comment, I declare to edit the question. Attach the file structure and directories, as well as code that changed to shorten the procedure, in order to reveal the error. The predicted scenario is as follows: authorized associate since will log in, will open the main-page.php with optional menu of forms that are allowed to edit. This page immediately include ‘form-carrier.php with it self include some-data.php’. Once through anchor tag he will choose the appropriate form, this form should be included in the appropriate div with "id=include".

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)

Tested by specifying the path by typing, and discovered that it works properly if inscribe in require("forms-byclass1-subdir/form-some-name.php"); as well as "../forms-dir/ forms-byclass1-subdir/ form-some-name.php". This leads me to the conclusion that the "form-some-name.php" is registered by the main-page.php which is in working-dir, as well as by form-carrier.php located in forms-dir, which is a child of main- page.php. Not supposed to work like this, but it works. To avoid confusion I replaced two including files (form-carrier.php; and some-data-php) in same dir "working-dir" together with "main-page.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>
...

I shortened the code and decided not to go through the data base, but in javascript create variable with the full path and send it directly by AJAX to the target file. Again the result is the same. If inscribe the path by typing, the code is executed correctly, if I go with use of a variable in javascript, sent by Ajax, I get correct data, in alert(html) can read all data from the form-some-name.php, as well as form-carrier.php; and some-data.php, var_dump($strPath); gives me the correct path, but include div is still empty! I will be very grateful if anyone still interested contribute to solving this, for me unknown and strange problem.

解决方案

Since no one with sufficient knowledge, wants to spend time on solving this problem, and I am sure that is easily solvable, I solved for now and post hoping to find useful for other beginners in this area as me! I went back to the first approach. This way is much simpler and provides few following steps: page1 achor click>page1 js var for path and asign to intermediate filed in page1>js load page3 in page2 with filePath. The problem with premature refreshing fixed as follows: first with splitting JS file where were all the codes for those three files, in three for each page separately, also adding setTimeout function to the other procedures for executing with correct order. Now it’s running smoothly!

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);
});

This does not mean that I was gave up from approach, using Ajax, PHP and db, but until I find the way to fix it, this is certainly a good solution. Therefore remains the call for contribution from the gurus of web-design.

Note for proper functioning various events: because the file is dynamically included , will be problematic using of JQuery .click (), as well as JavaScript HTML DOM EventListener if it not used in html inline. The most reliable way, at least in my case, is using of Jquery ".on( events [, selector ] [, data ], handler )".

这篇关于PHP包含/需要使用可变路径的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆