Here is what I am trying to accomplish. I have a form that uses jQuery to call AJAX in a PHP file. The PHP file interacts with the database and then creates the contents of the page to return as an AJAX response; those. the contents of this page are written to a new window in the success function to call $.ajax . As part of the content of the page returned by the PHP file, I have a simple HTML script tag that has a JavaScript file in it. In particular:
<script type="text/javascript" src="pageControl.js"></script>
This is not reflected in php (although I tried this), it is just html. The pageControl.js file is in the same directory as my php file that generates the content.
No matter what I try, I can not get the pageControl.js file that is included or working in a new window that appears, created in response to success in an AJAX call. I get errors such as "Object expected" or a variable not defined, which leads to the file not being included. If I copy JavaScript directly into a PHP file, instead of using a script tag with src, I can make it work.
Is there something I am missing here about resolving the area between the calling file, php and jQuery AJAX? I am going to include javascript files this way in the future and would like to understand what I am doing wrong.
Hi again:
I dealt with this problem, and I still had no luck. I'm going to try to clarify what I'm doing, and maybe it will bring something to mind. I am including some code as requested to help clarify things a bit.
Here is the sequence:
- The user selects some parameters and clicks the submit button on the form.
Clicking a form click is processed by jQuery code, which 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";
This code makes an AJAX call to the PHP file ( process_report_request.php ), which creates the page content for a new window. This content is taken from the database and HTML. In a PHP file, I want to include another javascript file in my head using javascript, which is used in a new window. I am trying to enable it as follows
<script src="/folder1/folder2/folder3/pageControl.js" type="text/javascript"></script>
Changed path folder name to protect the innocent :)
The pageControl.js file is actually located in the same folder as the jQuery code file and php file, but I try to make the full path just safe. I can also access the js file using the URL in the browser, and I can successfully enable it on the html static test page using the src script 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, as soon as the php file sends the entire contents of the page back to an AJAX call, a new window will open, and the returned content will be written to it by jQuery code above.
The error โError: objectโ appears in the writePageControls line when I start the page. However, since JavaScript works fine both on a static HTML page and when including "inline" in a PHP file, it makes me think that this is a problem of some way.
Again, no matter what I try, my function calls in the pageControls.js file do not work. If I put the contents of the pageControl.js file in a php file between script tags and don't change anything, it works as expected.
Based on what some of you have already said, I wonder if permission is allowed correctly for a newly opened window. But I do not understand why, because I use the full path. Also, to further confuse issues, my linked stylesheet works great with a PHP file.
Sorry how long this has been going on, but if anyone has the time to look at it further, I would really appreciate it. I'm at a dead end. I'm new when it comes to a lot of this , so if there is only the best way to do this and avoid this problem, Iโm all ears (or eyes, I suppose ...)