ceruleansin Posted April 22, 2010 Share Posted April 22, 2010 I would love to find an elegant way to do the following sequence. I'm so new to PHP that its easier to ask because I don't know what to look for. Lets say I have the following files. index.php my_javascript.js The index file calls my Javascript in the normal fashion with the following code. <script src="my_javascript.js" type="text/javascript"></script> In my JS file there is a section which dynamically adds content to a <div> that is in the index.php file. I must use the JS file to add the content. HERE IS MY PROBLEM. How do I dynamically replace the content in the JS file BEFORE it gets sent to the user? EXAMPLE of index.php: <html> <head> <script src="my_javascript.js" type="text/javascript"></script> </head> <body> <div id="my_id"> This is static text that will be dynamically changed by Javascript. </div> </body> </html> EXAMPLE of my_javascript.js: replace_contents += 'This is the dynamic content of the div to be changed.'; So, I need to replace the line "This is the dynamic content of the div to be changed." with something from a php array before it gets sent to the user. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/199393-preload-a-javascript-file-in-php-for-dynamic-changes-then-output/ Share on other sites More sharing options...
JAY6390 Posted April 22, 2010 Share Posted April 22, 2010 Surely you would be better off using AJAX for this rather than trying to dynamically alter a JS file. It is possible, it's just unorthodox to be honest Quote Link to comment https://forums.phpfreaks.com/topic/199393-preload-a-javascript-file-in-php-for-dynamic-changes-then-output/#findComment-1046452 Share on other sites More sharing options...
ceruleansin Posted April 22, 2010 Author Share Posted April 22, 2010 Ok. That seems logical. I hadn't played with AJAX yet but I looked up some basics. I have a few Q's though about the process. Is this correct? 1) Server side scripts are run 2) Client data is sent 3) Web App (Firefox ect) displays client data and runs client side scripts. 4) User generates query and AJAX requests data. 5) Changed data is displayed. So, if I were creating a gallery and the user clicked on a thumbnail, then I could request any data from the server that I wanted via AJAX correct? Quote Link to comment https://forums.phpfreaks.com/topic/199393-preload-a-javascript-file-in-php-for-dynamic-changes-then-output/#findComment-1046488 Share on other sites More sharing options...
JAY6390 Posted April 22, 2010 Share Posted April 22, 2010 Correct, and the page would not reload, but you could get data and update content on your page still Quote Link to comment https://forums.phpfreaks.com/topic/199393-preload-a-javascript-file-in-php-for-dynamic-changes-then-output/#findComment-1046491 Share on other sites More sharing options...
ceruleansin Posted April 22, 2010 Author Share Posted April 22, 2010 Awesome! I should have guessed that there was a way to do this. Thanks very much. BTW, I checked out your site and I love the contents. It might just be me, however I would love to see a compressed version of the page with just the headers. I hated having to scroll down the huge page to see what other cool topics you had about PHP. Thanks again!! Quote Link to comment https://forums.phpfreaks.com/topic/199393-preload-a-javascript-file-in-php-for-dynamic-changes-then-output/#findComment-1046502 Share on other sites More sharing options...
JAY6390 Posted April 22, 2010 Share Posted April 22, 2010 I'll keep it in mind, I'm going to be redesigning it soon, thanks for the input Quote Link to comment https://forums.phpfreaks.com/topic/199393-preload-a-javascript-file-in-php-for-dynamic-changes-then-output/#findComment-1046505 Share on other sites More sharing options...
ceruleansin Posted April 22, 2010 Author Share Posted April 22, 2010 Jay, Just one more thing. I see people using CPAINT2 as a framework for using AJAX. If you know anything about it, can you tell me how I would go about using it to request a file from the server? I tried using my own AJAX script below: function loadXMLDoc() { 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("overlay_info_contents").innerHTML=xmlhttp.responseText; alert("yes this is active!"); } } xmlhttp.open("GET","http://ceruleansin.byethost3.com/gallery/libraries/ajax_info.txt",true); xmlhttp.send(); } I think it didn't work because CPAINT is already in use and there might be a conflict in the xmlhttp.onreadystatechange. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/199393-preload-a-javascript-file-in-php-for-dynamic-changes-then-output/#findComment-1046554 Share on other sites More sharing options...
JAY6390 Posted April 22, 2010 Share Posted April 22, 2010 I have no idea to be honest, never heard of it. Use JQuery as a framework Quote Link to comment https://forums.phpfreaks.com/topic/199393-preload-a-javascript-file-in-php-for-dynamic-changes-then-output/#findComment-1046557 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.