SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using javascript variable in php inside javascript function

    In my code a javascript function received value from html hyperlink. Value passing successfully, inside product function document.write(a) printing value 5.
    But how can i print value of "a" inside product function by php. My code is here....


    <html>
    <body>

    <a href='javascript: product(5);'>click</a>

    <script type='text/javascript'>

    function product(a)
    {
    document.write(a)

    document.write("<?php

    echo "some thing";

    ?>");

    }
    </script>
    </body>
    </html>

  2. #2
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP is a server side language, JavaScript is a client side language. They are doing completely different parts of serving your page, you can't mix them.

    Process works like this:
    1. Server generates page. <-- PHP
    2. Server sends page to client. <-- As soon as last byte of page is sent, server stops being involved in page generation. PHP can't be used after that.
    3. Browser receives page. <!-- Start of JavaScript involvement

    PHP and JavaScript are doing completely different things on different sides of serving the page. They can't interact.

  3. #3
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brother CyberAlien, inside php code javacript code can ran,

    <? php
    $mi="<script type='text/javascript'>document.write(5234);</script>";
    echo $mi; // print 5234
    ?>
    My problem is, inside javascript function passing javascript variable to php variable.

  4. #4
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, it doesn't run. You are just echoing text. Browser then treats text as script and runs it when you've sent whole page.

  5. #5
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in my main code i can not run javascript code inside php code....

    <html>
    <body>

    <a href='javascript: product(5);'>click</a>

    <script type='text/javascript'>

    function product(a)
    {
    document.write(a)

    document.write("<?php

    $mi="<script type='text/javascript'>document.write(a);</script>";
    echo $mi;

    ?>");

    }
    </script>
    </body>
    </html>

    this is my problem, $mi not printing anything

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Look at the rendered source code of the page and you will find that some fundamental HTML errors are getting in your way.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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