为什么我的<脚本>从PHP文件标签工作? (jQuery的参与也在这里) [英] Why doesn't my <script> tag work from php file? (jQuery involved here too)

查看:87
本文介绍了为什么我的<脚本>从PHP文件标签工作? (jQuery的参与也在这里)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是什么,我试图完成。我有一个使用jQuery来使一个AJAX调用到PHP文件的形式。 PHP文件交互的数据库,然后创建页面内容返回为Ajax响应;即本页面内容为$就调用写入到一个新窗口中签功能。作为由PHP文件返回页面内容的一部分,我有了一个JavaScript文件,一个简单的HTML脚本标记。具体做法是:

<脚本类型=文/ JavaScript的SRC =pageControl.js>< / SCRIPT>

这是不是在php呼应(虽然我已经试过了),它仅仅是HTML。该pageControl.js是在同一个目录中生成内容我的PHP文件。

无论我怎么努力,我似乎​​无法得到pageControl.js文件包含或以响应AJAX调用成功创建后的新窗口的工作。我结束了像预期的对象或变量没有定义的错误,导致我相信该文件没有得到包括在内。如果我直接复制的JavaScript到PHP文件,而不是使用脚本标记与SRC,我可以得到它的工作。

有我丢失的东西在这里对范围决议,要求文件,PHP和jQuery的AJAX的?我将要包括的JavaScript文件,这种方式在未来,想了解我在做什么错。

在此先感谢!


大家好:

我已经在这个问题上的工作了,仍然没有运气。我会尝试澄清我在做什么,也许这会带来些什么心态。我包括一些code的要求有助于澄清事情有点。

下面的顺序:

  1. 在用户选择一些选项,并点击提交表单按钮。
  2. 在表单按钮点击是jQuery的code,看起来像这样处理的:

 
    $(文件)。就绪(函数(){
        $(#runReport)。点击(函数(){

            VAR报告= $(#报告)VAL()。
            变种程序= $(#程序)VAL()。
            VAR会话= $(#会议)VAL()。
            VAR学生= $(#学生)VAL()。

            VAR dataString ='报告='+报+
                             '与程序='+计划+
                             '与会话='+会+
                             与学生='+的学生;

            $阿贾克斯({
                键入:POST,
                网址:process_report_request.php
                缓存:假的,
                数据:dataString,
                成功:函数(pageContent){
                    如果(pageContent){
                        $(#result_msg)。addClass(successMsg)
                            的.text(报告创建的。);

                        VAR windowFeatures =WIDTH = 800,菜单栏= YES,滚动条= 1,可调整大小= 1,状态= YES;

                        //打开一个新的报告窗口
                        变种reportWindow =的window.open(,newReportWindow,windowFeatures);

                        //添加报告数据本身从AJAX调用返回
                        reportWindow.document.write(pageContent);
                        reportWindow.document.close();
                    }
                    其他 {
                        $(#result_msg)。addClass(failedMsg)
                            的.text(报告创建失败。);
                    }
                }
            }); //结束Ajax调用
            //从点击函数返回false,以prevent正常提交处理
            返回false;
        }); //最后点选通话
    }); //准备结束通话
 

这code执行AJAX调用到PHP文件( process_report_request.php )创建的新窗口中的页面内容。此内容取自数据库和HTML。在PHP文件我想包括在头部,在新的窗口中使用JavaScript的另一个JavaScript文件。我想,如下所示将其纳入

 <脚本的src =/文件夹1 / FOLDER2 / folder3 / pageControl.js类型=文/ JavaScript的>< / SCRIPT>
 

改路径的文件夹名称,以保护无辜者:)

该pageControl.js文件实际上是在同一个文件夹中的jQuery code文件和PHP文件,但我想要的完整路径,只是为了安全起见。 我还可以使用URL在浏览器中访问该js文件,我可以使用脚本SRC标记成功地将其包含在一个静态的HTML测试页。

在JavaScript文件包含在PHP文件,我有打电话给其中一个功能如下(PHP的echo):

 回声'<脚本类型=文/ JavaScript的LANGUAGE =JavaScript的> writePageControls();< / SCRIPT>';
 

所以,一旦PHP文件将所有的页面内容返回给AJAX调用,那么新窗口打开,并返回的内容被写入它的jQuery的code以上。

的writePageControls行是我得到的错误。错误:预期的对象当我运行页的但是,由于JavaScript的正常工作,同时在静态的HTML页面,当包括内联在PHP文件,这是导致我认为这是某种形式的路径问题。

此外,不管我怎么努力,我的电话在pageControls.js文件中的功能不起作用。 如果我把pageControl.js文件的内容在脚本标记之间的PHP文件和其他人没有什么改变,它按预期工作。

基于什么一些你已经说过

,我想知道,如果路径解析到新打开的窗口是不正确的。但我不明白为什么,因为我使用的完整路径。此外事项更加迷惑,我的链接的样式表只是正常的PHP文件。

道歉这是怎么长的,但如果有人有时间来看看这进一步,我将不胜AP preciate它。我难倒。 我是新手,当涉及到了很多本,所以如果有只是一个更好的方式来做到这一点,避免这个问题,我所有的耳朵(或眼睛我想...)

再次感谢事先!

解决方案

我也有问题,类似的问题这一点,这是一个真正的头痛。下面的方法可能不是优雅,但它为我工作。

  1. 确保你的PHP文件,只是输出你想在你的 身体
  2. 加入了jQuery窗头动态
  3. 添加任何外部脚本文件窗头动态
  4. 使用jQuery的HTML在窗口的文档调用HTML()与你的身体上加载的内容,使脚本进行评估。

例如,在阿贾克斯的成功:

 成功:函数(pageContent){
        VAR windowFeatures =WIDTH = 800,菜单栏= YES,滚动条= 1,可调整大小= 1,状态= YES;
        变种reportWindow =的window.open(,newReportWindow,windowFeatures);

        //样板
        VAR样板=< HTML>< HEAD>< /头><身体GT;< / BODY>< / HTML>中;

        reportWindow.document.write(样板);

        VAR头= reportWindow.document.getElementsByTagName(头)[0];

        VAR的jQuery = reportWindow.document.createElement(脚本);
        jquery.type =文/ JavaScript的;
        jquery.src =HTTP://$c$c.jquery.com/jquery-1.7.min.js;
        head.appendChild(jQuery的);

        VAR JS = reportWindow.document.createElement(脚本);
        js.type =文/ JavaScript的;
        js.src =/folder1/folder2/folder3/pageControl.js;
        js.onload =功能(){
            reportWindow $(机构)HTML(pageContent)。
        };
        head.appendChild(JS);


        reportWindow.document.close();

    }
 

祝你好运!

Here is what I am trying to accomplish. I have a form that uses jQuery to make an AJAX call to a php file. The php file interacts with a database, and then creates the page content to return as the AJAX response; i.e. this page content is written to a new window in the success function for the $.ajax call. As part of the page content returned by the php file, I have a straightforward html script tag that has a javascript file. Specifically:

<script type="text/javascript" src="pageControl.js"></script>

This is not echoed in the php (although I have tried that), it is just html. The pageControl.js is in the same directory as my php file that generates the content.

No matter what I try, I can't seem to get the pageControl.js file included or working in the resulting new window created in response to success in the AJAX call. I end up with errors like "Object expected" or variable not defined, leading me to believe the file is not getting included. If I copy the javascript directly into the php file, rather than using the script tag with src, I can get it working.

Is there something I am missing here about scope resolution between calling file, php, and the jQuery AJAX? I am going to want to include javascript files this way in the future and would like to understand what I am doing wrong.

Thanks in advance!


Hello again:

I have worked away at this issue, and still no luck. I am going to try and clarify what I am doing, and maybe that will bring something to mind. I am including some code as requested to help clarify things a bit.

Here is the sequence:

  1. User selects some options, and clicks submit button on form.
  2. The form button click is handled by jQuery code that looks like this:


    $(document).ready(function() {
        $("#runReport").click(function() {

            var report = $("#report").val();
            var program = $("#program").val();
            var session = $("#session").val();
            var students = $("#students").val();

            var dataString = 'report=' +report+ 
                             '&program=' +program+
                             '&session=' +session+
                             '&students=' +students;

            $.ajax({
                type: "POST",
                url: "process_report_request.php",
                cache: false,
                data: dataString,
                success: function(pageContent) {
                    if (pageContent) {
                        $("#result_msg").addClass("successMsg")
                            .text("Report created.");

                        var windowFeatures = "width=800,menubar=yes,scrollbars=1,resizable=1,status=yes";

                        // open a new report window
                        var reportWindow = window.open("", "newReportWindow", windowFeatures);

                        // add the report data itself returned from the AJAX call
                        reportWindow.document.write(pageContent);
                        reportWindow.document.close();
                    }
                    else {
                        $("#result_msg").addClass("failedMsg")
                            .text("Report creation failed.");
                    }
                }
            }); // end ajax call
            // return false from click function to prevent normal submit handling
            return false;
        }); // end click call    
    }); // end ready call

This code performs an AJAX call to a php file (process_report_request.php) that creates the page content for the new window. This content is taken from a database and html. In the php file I want to include another javascript file in the head with javascript used in the new window. I am trying to include it as follows

<script src="/folder1/folder2/folder3/pageControl.js" type="text/javascript"></script>

Changed path folder names to protect the innocent :)

The pageControl.js file is actually in the same folder as the jQuery code file and the php file, but I am trying the full path just to be safe. I am also able to access the js file using the URL in the browser, and I can successfully include it in a static html test page using the script src tag.

After the javascript file is included in the php file, I have a call to one of its functions as follows (echo from php):

 echo '<script type="text/javascript" language="javascript">writePageControls();</script>';

So, once the php file sends all the page content back to the AJAX call, then the new window is opened, and the returned content is written to it by the jQuery code above.

The writePageControls line is where I get the error "Error: Object expected" when I run the page. However, since the javascript works fine in both the static html page and when included "inline" in the php file, it is leading me to think this is a path issue of some kind.

Again, no matter what I try, my calls to the functions in the pageControls.js file do not work. If I put the contents of the pageControl.js file in the php file between script tags and change nothing else, it works as expected.

Based on what some of you have already said, I am wondering if the path resolution to the newly opened window is not correct. But I don't understand why because I am using the full path. Also to confuse matters even more, my linked stylesheet works just fine from the php file.

Apologies for how long this is, but if anyone has the time to look at this further, I would greatly appreciate it. I am stumped. I am a novice when it comes to a lot of this, so if there is just a better way to do this and avoid this problem, I am all ears (or eyes I suppose...)

Thanks again in advance!

解决方案

I have also had problems with a similar issue to this, and this was a real headache. The following approach may not be elegant, but it worked for me.

  1. Make sure that your php file, just outputs what you want in your body
  2. Add jquery to the window head dynamically
  3. Add any external script files to the window head dynamically
  4. use jQuery html on the window's document to call html() with your loaded content on the body, so that scripts are evaluated.

For example, in your ajax success:

success: function(pageContent) {
        var windowFeatures = "width=800,menubar=yes,scrollbars=1,resizable=1,status=yes";
        var reportWindow = window.open("", "newReportWindow", windowFeatures);

        // boilerplate
        var boilerplate = "<html><head></head><body></body></html>";

        reportWindow.document.write(boilerplate);

        var head = reportWindow.document.getElementsByTagName("head")[0];

        var jquery = reportWindow.document.createElement("script");
        jquery.type = "text/javascript";
        jquery.src = "http://code.jquery.com/jquery-1.7.min.js";
        head.appendChild(jquery);

        var js = reportWindow.document.createElement("script");
        js.type = "text/javascript";
        js.src = "/folder1/folder2/folder3/pageControl.js";
        js.onload= function() {
            reportWindow.$("body").html(pageContent);
        };
        head.appendChild(js);


        reportWindow.document.close();

    }

Good luck!

这篇关于为什么我的&LT;脚本&GT;从PHP文件标签工作? (jQuery的参与也在这里)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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