1

我会这样说开始:我对这一切都很陌生。我真的只是通过我能找到的一切崩溃。我是一名艺术家和作家,我只是在去年才真正开始开发这些东西。

我正在开发一个项目,它结合了 cms、项目经理和数据库前端(以及我的小组可能想要添加的任何其他内容)。我正在用 php、mysql 和 javascript 构建它。但是我在建立基金会时遇到了麻烦。

我正在尝试测试一些东西,首先是使用 AJAX 为数据后端调用 php 脚本的 mysql 查询。

这个项目有两个文件夹,一个私有的和一个公共的。一切都通过一个 index.php 引导文件。我有一个看起来像这样的 .htaccess mod_rewrite:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]

</IfModule>
<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>
  php_value include_path "../private/libs/classes/:../private/config/:../private/config/inc/:../private/libs/smarty/:../private/libs/scripts/:../private/libs/smarty/libs/:../private/libs/smarty/libs/sysplugins/:../private/libs/smarty/libs/plugins/"

我这里有一点javascript,应该调用php脚本showCode.php:

<script>
    function showCode(str)
    {
        if (str=="")
          {
              document.getElementById("code").innerHTML="";
              return;
          }
        if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("code").innerHTML=xmlhttp.responseText;
                }
          }
        xmlhttp.open("POST","showCode.php",false);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("id="+str);
    }
</script>

我不关心我的php。我已经分别测试了所有脚本并且它们工作正常,我的引导文件也从未给我带来任何麻烦。我有一个相对复杂的加载系统,它一直运行良好。

我有一种严重的预感,就是 .htaccess 给我带来了麻烦。mod_rewrite 可能导致 ajax 无法到达 showCode.php 文件,但对于我来说,我无法找到答案。

有什么建议么?

我希望得到任何帮助,但我不是一个使用 jquery 的人......我真的很想学习基础 javascript 的来龙去脉。一旦我对这一点足够熟练,我就会去图书馆。

- 编辑 - - - - - - - - - -

好的,感谢我在这里得到的一些有用的建议,我发现 .htaccess 不是我的问题。我在想,无论我通过 AJAX 函数引入的任何代码都将使用我以前的 PHP 设置(我通过引导程序引入的所需文件)……这只是简单地考虑一下,这是荒谬的,因为 php 在服务器上运行并且AJAX 调用只是重新加载客户端上的 div。所以......现在我必须相应地修复我的代码......

- 最后 - - - - - - - - -

是的...我需要在我的 showCode 脚本中包含 baseConfig 脚本。现在一切正常, .htaccess 重写到位。感谢您为我指明正确的方向。

4

1 回答 1

0

如果您认为您在使用某种脚本或文件时遇到问题,请想办法在没有该脚本或文件的情况下尝试整个设置......在我的情况下,我认为是 .htacces 是罪魁祸首。原来与那个文件没有任何关系,我只有在我注释掉我的重写后才能发现它。

所以,这个问题的真正答案是......当您对另一个需要通过引导程序加载的逻辑或其他文件的脚本进行 AJAX 调用时......不要假设引导程序会加载它。当然,PHP 是一种服务器端语言。当您调用 AJAX 函数时,您正在从客户端执行此操作,原始引导程序已经执行完毕......在被调用的 php 中需要您需要的任何内容,或者创建某种 ajax 引导程序文件,您可以通过该文件加载任何脚本通过javascript函数。这是一个简单的例子......或者可能不是那么简单的例子......无论哪种方式,这是我的例子:

Javascript:

    function ajaxLoader(post,script,div){

    if (post==""){
        document.getElementById(div).innerHTML="";
        return;
    }

    if (window.XMLHttpRequest){
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }else{
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById(div).innerHTML=xmlhttp.responseText;
        }
    }

    xmlhttp.open("POST","/includes/scripts/ajaxBootstrap.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

    /* This is a very simple one, with only 'id' being sent to the script 
       by the sender below. With more effort, I'm sure you could make it send 
       multiple data by taking out "&id=" and only working with the post variable*/ 

    xmlhttp.send("script="+script+"&id="+post);
    }

PHP ajax 引导程序:

加载所有必要的脚本/函数/包含/等,而不必包含每个新脚本

<?php
    // require configuration
    require_once('config.php');

    // test whether the $_POST array has a key called 'script', which should be set in the javascript function.
    IF(array_key_exists('script',$_POST)){
        foreach($_POST as $key=>$data){
        // set variable variables from the $_POST data
            $$key = $data;
        }
        // include the called script
        include($script.".php");
    }ELSE{
    // place any error messages you'd like to show.
    }
?>

另外,我不确定这是否有必要(可能可以在配置文件中使用 ini_set 来规避这一点),但我发现我必须放置一个带有补偿的 php include_paths 的 .htaccess 文件,因为引导程序被放置在不同的文件夹比我的 index.php 文件。

PHP 脚本:

这才是真正执行我们想要的代码

<?php
    $form = new forms;
    $id = $form->getPOST('id');
    $connection = new dbPortal;
    eval($connection->showSingle('pages', 'page_content', $id));
 ?>

我已经对此进行了测试,当然,它对我来说非常有效,包括我的课程和其他我没有在这里展示的东西。

于 2013-06-18T22:13:13.983 回答