Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Generating PHP code with JavaScript

    Hi,
    I'm struggling with something that I'd appreciate some help with.
    I have a js file called functions.js, in it I have the following function:
    Code:
    function PopulateSelectElements()
    			{
    			//Generating a SELECT using php (called via javascript)
    			var strcode = "<?php GenerateSelect('rfi', 'Age', 'width: 100%', 'project', 'age'); ?>";
    			var txt=document.getElementById("projects");
    			
    			// check if the element is truthy, meaning that the element does eixst on the form and is not null.
    			if(txt)
    			{
    			txt.innerHTML=strcode;
    			}
    			else
    			{
    			alert("null");
    			}
    			}
    What this does is basically looking for the given elements and writes the php code in it. The element in my HTML is a td:
    Code:
    <td id="projects" colspan="3"></td>
    In my HTML I have the following to call the function:
    Code:
    <body onload="PopulateSelectElements()">
    while having this in the head:
    Code:
    <script src="functions.js" type="text/javascript"></script>
    For some reason the output of the javascript is commented out when the page is loaded!
    Code:
    <td id="projects" colspan="3"><!--?php GenerateSelect('rfi', 'Age', 'width: 100%', 'project', 'age'); ?--></td>
    However, if I move the function (PopulateSelectElements) from the js file to the head of the HTML file, then it won't be commented out!
    Could someone please help me with this? I want to have the function in js file so my code would be easier to manage.

    I also have tried to have the following the the head ... but the result is the same.
    Code:
    window.onload = PopulateSelectElements;
    Any help is greatly appreciated.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    First of all, you have it bassackwards.

    You can *NOT* generate PHP code *FROM* Javascript.

    You *CAN* generate JavaScript code *FROM* PHP.

    But this will never work:
    I have a js file called functions.js, in it I have the following function:
    Code:
    function PopulateSelectElements()
    {
    	//Generating a SELECT using php (called via javascript)
    	var strcode = "<?php GenerateSelect('rfi', 'Age', 'width: 100%', 'project', 'age'); ?>";
    Or, rather it could work. But *ONLY* if you named the file functions.js.php

    PHP code will *NOT* be executed unless it is in a file named ".php". (Yes, there are ways around this, but one if them is *NOT* to use a file named ".js" for reasons way too long to go into.)

    And then all this WILL work. You just have to change to using
    Code:
    <script src="functions.js.php" type="text/javascript"></script>
    And, yes, is perfectly legal to use *ANY FILENAME* in a <script src="...">.

    The browser won't care.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    If it's not obvious, the name of the file really doesn't matter, so long as it ends in ".php"
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    To make certain that the browser will correctly interpret it as a javaScript file despite the .php suffix you can set the MIME type header in the PHP as well as specifying it in the script tag.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,974
    Thanks
    15
    Thanked 229 Times in 229 Posts
    There's a much better way of doing what you want to do. As it is, JS and php barely speak to each other, one is on the server, the other the client. Js can speak to php through form inputs, and php can echo things into JS blocks. But there is one way they can communicate on equal terms, through the common cookie. Both can make and read cookies, equally well. So have one put the info on a cookie, and the other one reads it and uses it. But since I don't know exactly why you want to do this in this way, there may be yet other ways of doing it that are better.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    Quote Originally Posted by felgall View Post
    To make certain that the browser will correctly interpret it as a javaScript file despite the .php suffix you can set the MIME type header in the PHP as well as specifying it in the script tag.
    While I absolutely agree with you in theory, in practice I've never needed to do that. (I have done it on occasion, but not often.)

    I generate custom JavaScript via ASP code all the time, as I think I've mentioned before, and it works a charm, with or without the explicit MIME type in the headers.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    Quote Originally Posted by Old Pedant View Post
    First of all, you have it bassackwards.

    You can *NOT* generate PHP code *FROM* Javascript.

    You *CAN* generate JavaScript code *FROM* PHP.

    But this will never work:

    Or, rather it could work. But *ONLY* if you named the file functions.js.php

    PHP code will *NOT* be executed unless it is in a file named ".php". (Yes, there are ways around this, but one if them is *NOT* to use a file named ".js" for reasons way too long to go into.)

    And then all this WILL work. You just have to change to using
    Code:
    <script src="functions.js.php" type="text/javascript"></script>
    And, yes, is perfectly legal to use *ANY FILENAME* in a <script src="...">.

    The browser won't care.
    Thanks for your reply.

    I changed the file extension from .js to .js.php. The reference is also changed, but when I refresh the page, nothing is printed, even the commented php code. I'm using XAMPP for development if that makes any difference.

    I have a feeling that what I'm doing might not be really smart. I'm gonna explain what I have in mind, hoping that you can suggest me a better way to achieve my goal.

    I want to generate SELECT inputs when the page is loaded. the SELECT OPTIONS should be populated from db since it might change in time. So, I can't hard code the options, I need to retrieve the list from MySQL and populate the SELECT.

    On way is to directly use the function that I already have in my PHP files:
    PHP Code:
    <?php GenerateSelect('rfi''Age''width: 100%''project''age'); ?>
    This works fine, but the thing is, I want to generate these PHP files using PHP scripts. Something like CMS. the header, body and footer of these documents will be stored in DB and generated by php scripts.
    In the document, there will many SELECT inputs with different OPTIONS. Notice the parameters of the function GenerateSelect. So, one way would be to hard code every SELECT and store them in the DB and let the generator scripts to generate the whole page, but maintain that kind of code will be very difficult, specially if there's going to be changes later on. I wanted to somehow automate this process, have a function to find the elements that require to have SELECT and populate OPTIONS for them. For example, In my form I'll have tds with specific ids (project, users or whatever) then I'll have to GenerateSelects with proper parameters ...

    This might have become a bit complicated, sorry If I didn't explain it nicely. So, do you have any suggestions in achieving what I have in mind?

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    Well, of course you would need to copy all the code that supports GenerateSelect( ) into the ".js.php" file. Or include it ("require" in PHP parlance, I believe).

    Just because the main page is in PHP code that doesn't mean the ".js.php" file can call code in it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Old Pedant View Post
    Or include it ("require" in PHP parlance, I believe).
    PHP has both include and require - the difference being that if you use include and it can't find the file then it continues running without it.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    Quote Originally Posted by felgall View Post
    To make certain that the browser will correctly interpret it as a javaScript file despite the .php suffix you can set the MIME type header in the PHP as well as specifying it in the script tag.
    browsers ignore the mime type of js urls, so this step is a waste of time...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #11
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by rnd me View Post
    browsers ignore the mime type of js urls, so this step is a waste of time...
    No they don't.

    If you specify the correct MIME type on JavaScript - application/javascript - then old versions of IE will not run the script at all as they will only run it if you use the long obsolete - text/javascript - instead. Of course once IE8 is dead there will no longer be any reason to continue to use the obsolete text/javascript and you can specify the correct application/javascript on all your script tags - you will need to specify it in your code unless you update the server settings to ensure that all .js files get served correctly as application/javascript.

    Also some browsers will not run a JavaScript file even if you do specify text/javascript in the HTML if the filename ends in .php - you need to have the MIME type set in the file header of the actual file for those to recognise that the file is JavaScript and not PHP. I have actually had instances where not supplying the text/javascript or application/javascript header inside the PHP file means that the browser doesn't run the JavaScript at all.
    Last edited by felgall; 07-10-2013 at 10:48 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #12
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    Quote Originally Posted by rnd me View Post
    browsers ignore the mime type of js urls, so this step is a waste of time...
    I think I have to agree with this.

  • #13
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    Quote Originally Posted by felgall View Post
    If you specify the correct MIME type on JavaScript - application/javascript - then old versions of IE will not run the script at all as they will only run it if you use the long obsolete - text/javascript - instead.
    i serve a lot of my scripts as text/html so as to display formatted inline documentation or re-direct the user to a document. never had a problem with IE refusing those scripts. I prefer "text/javascript" since it's more descriptive of the content than application. if html is text and css is text, why shouldn't javascript be text? the other benefit to "text/plain" or "text/javascript" is that is shows up in the url bar instead of coercing a download, making "walk-up" debugging easier. it also runs on all machines, which trumps any other argument.

    it's best practice to use "text/javascript", no matter what librarians will argue. it's how all the pros serve their js. for example, here is a wikipedia script that both ends in .php and is served under "text/javascript":
    https://bits.wikimedia.org/en.wikipe...0710T023248Z&*

    if there were really big problems with either of those two things, the number 5 site on the net wouldn't use them...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •