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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to pass php value to javascript?

    Here is my code


    <script language="javascript" type="text/javascript">
    function revealModal(divID)
    {

    window.onscroll = function () { document.getElementById(divID).style.top = document.body.scrollTop; };
    document.getElementById(divID).style.display = "block";
    document.getElementById(divID).style.top = document.body.scrollTop;

    }

    function hideModal(divID)
    {
    document.getElementById(divID).style.display = "none";
    }
    </script>


    <div id="modalPage3">
    <div class="modalBackground"></div>
    <div class="modalContainer">
    <div class="modal53">
    <div class="modalTop"><a href="javascript:hideModal('modalPage3')">[X]</a></div>
    <div class="modalBody">
    <?php echo 'value='.$_SERVER['value'];


    ?>
    <h3><center>Choose from the options below</center></h3>
    <center><div id="stylized" class="myform">

    <a href="javascript:hideModal('modalPage3');javascript:revealModal('modalPage1')" tabindex="2" title="Remove UPCAT Passer"><img src="images/photo_remove.png" /></a>&nbsp;&nbsp;
    <a href="javascript:hideModal('modalPage3');javascript:revealModal('modalPage2')" tabindex="4" title="Edit UPCAT Passer"><img src="images/photo_edit.png" /></a>&nbsp;&nbsp;
    <a href="javascript:hideModal('modalPage3');javascript:revealModal('modalPage4')" tabindex="4" title="Change Status of UPCAT Passer"><img src="images/photo_up.png" /></a>
    </div></center>
    </div>
    </div>
    </div>
    </div>


    <a href=javascript:revealModal('modalPage3');>
    <img src=images/option.jpg /></a>

    my problem is this one..how can I pass a value example value=3 to be like
    href= index.php?value=3..PLEASE HELP!!!

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,301
    Thanks
    23
    Thanked 612 Times in 611 Posts
    php to js is a simple thing, just echo in the js.
    Part one php and js only:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>New document</title>
    <?php
    $var_we_pass = '3';
    ?>
    
    <script type="text/javascript">
    
    href= 'index.php?value=' + <?php echo $var_we_pass; ?>;
    alert(href);
    </script>
    Part two add php to the HTML:
    Code:
    </head>
    
    <body>
    
    <a href="javascript:hideModal('modalPage3');javascript:revealModal('modalPage4')" tabindex="<?php echo $var_we_pass; ?>" title="Change Status of UPCAT Passer">
    <img src="images/photo_up.png" /></a>
    
    </body>
    </html>

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    627
    Thanks
    1
    Thanked 20 Times in 20 Posts
    Move your php-echo line up a few lines so that it is within the script:
    Code:
    <?php echo 'value='.$_SERVER['value'];?>

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    If you use

    <script type="text/javascript" src="scriptname.php">

    to attach the JavaScript to the page then the file can contain PHP that generates the JavaScript code dynamically.
    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.


  •  

    Posting Permissions

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