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 11 of 11
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Glasgow, Scotland
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Including PHP within Javascript code

    Is is possible to include PHP code within a section of Javascript? The code below shows that I am trying to include the PHP echo command in the following javascript function but it does not work.

    Many thanks,

    Martin


    <script language="JavaScript">
    function echoSession(num)
    {
    window.document.newForm.mybox.value=num;
    <?PHP echo("string"); ?>
    }
    </script>

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    It worked just fine for me. You are running this in a .php file right?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New Coder
    Join Date
    Jul 2002
    Location
    Glasgow, Scotland
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, its a .php file. The code I am using is in this format but doesnt seem to echo anything to the screen.

    <html>

    <script language="JavaScript">
    function echoSession(num)
    {
    window.document.newForm.mybox.value=num;
    <?PHP echo("string"); ?>
    }
    </script>


    <?PHP
    require("functions.php");
    adminHeader($cookie);
    makeConnection();

    $numSessions=1;

    echo("<INPUT TYPE=BUTTON onClick=\"echoSession(".$numSessions.");\" NAME='addSession' VALUE='Add a session!'>");

    ?>

    </html>
    Last edited by martinjsmith; 07-24-2002 at 04:36 PM.

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    The echo command doesn't necessarily mean print it to the screen. It just echos it to the file it's in. In the case of your first post it echoed it within the javascript. This is the output of that:

    <script language="JavaScript">
    function echoSession(num)
    {
    window.document.newForm.mybox.value=num;
    string
    }
    </script>


    As for your second post.....it works just fine as well. Here is the output of that:

    <script language="JavaScript">
    function echoSession(num)
    {
    window.document.newForm.mybox.value=num;
    string
    }
    </script>


    <INPUT TYPE=BUTTON onClick="echoSession(1);" NAME='addSession' VALUE='Add a session!'>



    Note that if you do not put that input tag inside a form then it will not show up in Netscape4.x
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    New Coder
    Join Date
    Jul 2002
    Location
    Glasgow, Scotland
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, that makes sense now but what I really wanted to do all along is EXECUTE A PHP FUNCTION on the click of the 'addSession' button. As you saw before I tried to execute a javascript function on clicking the button and then using PHP within that javascript function. Based on your previous post I dont think that will work. Do you know of any way to execute a PHP function (say, phpFunction($num) below) on the click of a button?

    Many thanks,

    Martin


    <html>

    <script language="JavaScript">
    function echoSession(num)
    {
    window.document.newForm.mybox.value=num;
    <?PHP echo("string"); ?>
    }
    </script>


    <?PHP
    require("functions.php");
    adminHeader($cookie);
    makeConnection();

    function phpFunction($num)
    {
    echo("<TR><TD><SELECT NAME='signUpListDay[$num]'>");
    echoDays();
    echo("</SELECT></TD>");
    echo("<TD><SELECT NAME='signUpListTime[$num]'>");
    echoTimes();
    echo("</SELECT></TD>");
    echo("<TD><INPUT TYPE=TEXT NAME='signUpListLocation[$num]' SIZE=40></TD>");
    echo("<TD><INPUT TYPE=TEXT NAME='signUpListWeeks[$num]' SIZE=15></TD>");
    echo("<TD><a href='#' onClick=\"window.document.newForm.elements['signUpListDay[$num]'].value='';
    window.document.newForm.elements['signUpListTime[$num]'].value='';
    window.document.newForm.elements['signUpListLocation[$num]'].value='';
    window.document.newForm.elements['signUpListWeeks[$num]'].value='';\">Reset</a></TD></TR>");
    }

    $numSessions=1;

    echo("<INPUT TYPE=BUTTON onClick=\"echoSession(".$numSessions.");\" NAME='addSession' VALUE='Add a session!'>");

    ?>

    </html>

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To understand why your approach is not working, it's really important to remember that JavaScript runs on the client and PHP on the server. As Spookster has already said above, when PHP is done with processing a file, it sends the output as a HTTP response back to the client. So generally, these two technologies can't be interoperable, since they don't run in the same environment.

    Nevertheless, you can trigger PHP scripts through JavaScript by doing a simple operation that includes a GET request:

    var testImg = new Image();
    testImg.src = "http://adomain.com/test.php";

    This will run the script on the server, but you don't get any output aside from image information back to the client.

    With a load of DHTML and a small PHP backend you can achieve something called Remote Scripting. Heres a good resource that contains detailed info about how to use this method.

    For your script, it's purpose is not quite clear to me. I would favor a different approach that involves a refresh of the page, so when you click the button a new page gets loaded with the appropriate new output derived from the PHP function.

    Hope that cleared things up.

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    PHP is parsed serverside and your button (& associated script) is clientside so basically you can not do that.

    All PHP code is parsed before the HTML & Script ever gets to the users browser so no direct interaction is possible.

    (you can use hidden frames etc to emulate this but its really a pain)
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    New Coder
    Join Date
    Jul 2002
    Location
    Glasgow, Scotland
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Many thanks guys, I now understand the client/server side reasoning why it isnt possible. Im now starting work on a way to do it by refreshing the page as suggested by Mordred.

    Thanks again,

    Martin

  • #9
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    doh ! - did not see your post there Mordred which explained it better than mine - you beat me by 1 minute... but I reckon it was closer to 30 secods or so
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #10
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello!
    How to call to a function in javascript sending a numerical parameter? This parameter will be used in php code inside javascript:

    call the javascript function:
    <A href='javascript:void(0)' onClick='ShowFAQ(25)'>text</A>

    <script type="text/javascript">
    function ShowFAQ(faqid){
    window.document.labeltitlefaq.innerText='<?php echo($arrayFAQtitle[faqid])?>' ;
    }
    </script>

    The previous code does not work, and if I replace "faqid" to a number, it works fine.

    Thanks for your support

  • #11
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Besides this topic being three years old, the reason was explained above. PHP is a server-side language. This means that when you load the page, the webserver runs the code through PHP, then PHP sends output back, and the webserver sends it to you, thus using javascript (a client-side language) to call PHP code will not work, as the code will have already been removed and the output in its place. You could load the faq elements into a javascript array with PHP, which would mean the data persisted in the form of javascript. Or if you wanted to load it on demand, you could look into XMLHTTP Requests.
    "$question = ( to() ) ? be() : ~be();"


  •  

    Posting Permissions

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