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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Pass php variable to javascript function

    Hello all

    I have a big problem

    I have my php like this

    Code:
    <?
    $codid=$_GET["cid"];
    
    echo "
    <script>
    setTimeout(function() {CSelect();}, 100) ;
    </script>
    <div id=\"ReloadThis\"></div>";
    ?>
    And my js funtion

    Code:
    function CSelect()
    			{
    				var
    					$http,
    					$self = arguments.callee;
    
    
    				if (window.XMLHttpRequest) {
    					$http = new XMLHttpRequest();
    				} else if (window.ActiveXObject) {
    					try {
    						$http = new ActiveXObject('Msxml2.XMLHTTP');
    					} catch(e) {
    						$http = new ActiveXObject('Microsoft.XMLHTTP');
    					}
    				}
    
    				if ($http) {
    					$http.onreadystatechange = function()
    					{
    						if (/4|^complete$/.test($http.readyState)) {
    							document.getElementById('ReloadThis').innerHTML = $http.responseText;
    							setTimeout(function(){$self();}, 10);
    						}
    					};
    					$http.open('GET', 'linii_c/select.php'+'?cod='+'PHP VARIABLE', true);
    					$http.send(null);
    				}
    
    			}

    who i can put my php variable into the funtion?

    Thanx in advence and sorry for my bad english!

  • #2
    Regular Coder
    Join Date
    Jun 2011
    Posts
    103
    Thanks
    0
    Thanked 13 Times in 13 Posts
    1. in your javascript function, do not prepend your variables with a dollar sign ($), as this is invalid syntax for JS.

    2. instead of using PHP to grab the URL parameter, you can use javascript, refer here

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Quote Originally Posted by Fugix View Post
    1. in your javascript function, do not prepend your variables with a dollar sign ($), as this is invalid syntax for JS.
    No it isn't

    Even most of the JS frameworks use the $ notation

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,195
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    You can put the PHP variable in place in JavaScript code. Not hard.
    Code:
    	$http.open('GET', 'linii_c/select.php?cod=<?php echo $PHPVARIABLE; ?>', true);

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,195
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    As a minor point, though this is legal:
    Code:
    <script>
    setTimeout(function() {CSelect();}, 100) ;
    </script>
    you can instead do
    Code:
    <script type="text/javascript">
    setTimeout( CSelect, 100 );
    </script>

  • #6
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if i put
    Code:
    <script>
    setTimeout(function() {CSelect(".$codid.");}, 100) ;
    </script>
    and function

    Code:
    function CSelect(codid)
    			{
    				var
    					$http,
    					$self = arguments.callee;
    
    
    				......................
    ......................................................
    
    					$http.open('GET', 'linii_c/select.php'+'?cod='+codid, true);
    					$http.send(null);
    				}
    
    			}
    Returd (undefined) :|

  • #7
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    You can put the PHP variable in place in JavaScript code. Not hard.
    Code:
    	$http.open('GET', 'linii_c/select.php?cod=<?php echo $PHPVARIABLE; ?>', true);
    JS is not in php directory

  • #8
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    I don't get it. What do you want to tell us?

    Fact:
    Javascript is being generated by PHP in the first place ... much the same as HTML. Something like
    Code:
    <?php
       echo "<p>Whatever</p>";
    ?>
    <h3>Direct HTML</h3>
    <script type="text/javascript">
       var myJSVariable = <?php echo $PHPVARIABLE; ?>;
    </script>
    And this is exactly the way to output a PHP variable to javascript


  •  

    Posting Permissions

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