PHP Variable in Javascript

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
easinewe
Forum Newbie
Posts: 23
Joined: Mon Jan 11, 2010 3:22 pm

PHP Variable in Javascript

Post by easinewe »

I would like to pass a php variable to javascript but am having some trouble doing so. Below is my code. I would like to pass the php variable article[series] to the javascript. Could you help me understand what I am doing wrong. Is it possible to use php variables in javascript?

Code: Select all

    <? 
    $id = intval($_REQUEST["id"]);
    $sql = "SELECT * FROM `Catalog` WHERE id=$id";
    $result = mysql_query($sql);
    $article = mysql_fetch_array($result);
    echo article[series];
    ?>
 
<script type='text/javascript'>
 
 
function Overlay_Cover(){ 
    if(!document.getElementsByTagName)return;
    var arrImages = document.getElementsByTagName("img");
    var arrImgToFix = new Array();
    var zImages;
    var classCount = 0;
    for(var i=0; i<arrImages.length; i++){
        if(arrImages[i].className=="<?php echo article[series];?>_overlay"){
            arrImgToFix[classCount]=arrImages[i];
            classCount++;
        }   
    }
    for(var x = 0, y = arrImages.length; x < y; x++){   
        if(arrImgToFix[x]){
        arrImgToFix[x].style.background="url("+arrImgToFix[x].src+")";
// we go two ways: non IE Win and IE Win
            if (!arrImgToFix.push || !document.all){
                arrImgToFix[x].src="images/large/play_<?php echo article[series];?>.png";
            }else{
                arrImgToFix[x].style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',src='images/large/play_<?php echo article[series];?>.png')";     
                arrImgToFix[x].src="img/transparent.gif";
            }
        }
    }
}
window.onload=Overlay_Cover;
 
 
</script>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Variable in Javascript

Post by AbraCadaver »

For one you're missing the $:

Code: Select all

<?php echo $article['series']; ?>
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
easinewe
Forum Newbie
Posts: 23
Joined: Mon Jan 11, 2010 3:22 pm

Re: PHP Variable in Javascript

Post by easinewe »

Okay, I made the change regarding the "$", but I'm still having trouble.

I'm just posting a snippet of the code. Am I placing the php variable correctly in the javascript.


Code in my php would be this

Code: Select all

<?php
$article['series'] = chicago
?>
 
<script type='text/javascript'>
if(arrImages[i].className=="<?php echo $article['series'];?>_overlay")
</script>
I would like it to output the javascript like this

Code: Select all

<script type='text/javascript'>
if(arrImages[i].className=="chicago_overlay")
</script>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Variable in Javascript

Post by AbraCadaver »

Looks fine. What does it output?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: PHP Variable in Javascript

Post by kaszu »

Code: Select all

$article['series'] = 'chicago';  //quotes?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Variable in Javascript

Post by AbraCadaver »

kaszu wrote:

Code: Select all

$article['series'] = 'chicago';  //quotes?
Yes, other than that :oops:
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
easinewe
Forum Newbie
Posts: 23
Joined: Mon Jan 11, 2010 3:22 pm

Re: PHP Variable in Javascript

Post by easinewe »

I got it to work. Thanks for your help.

I was trying to echo an array item but i had to attach a variable to that specific array earlier in the at a specific Select statement.


I set this first

Code: Select all

<? $thisseries = $article['series'];?>

And then output this...

Code: Select all

<script type='text/javascript'>
 
 
function Overlay_Cover(){
    if(!document.getElementsByTagName)return;
    var arrImages = document.getElementsByTagName("img");
    var arrImgToFix = new Array();
    var zImages;
    var classCount = 0;
    for(var i=0; i<arrImages.length; i++){
        if(arrImages[i].className=="overlay_<?php echo $thisseries; ?>"){
            arrImgToFix[classCount]=arrImages[i];
            classCount++;
        }  
    }
    for(var x = 0, y = arrImages.length; x < y; x++){   
        if(arrImgToFix[x]){
        arrImgToFix[x].style.background="url("+arrImgToFix[x].src+")";
// we go two ways: non IE Win and IE Win
            if (!arrImgToFix.push || !document.all){
                arrImgToFix[x].src="images/large/play_<?php echo $thisseries; ?>.png";
            }else{
                arrImgToFix[x].style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',src='images/large/play_<?php echo $thisseries; ?>')";    
                arrImgToFix[x].src="img/transparent.gif";
            }
        }
    }
}
window.onload=Overlay_Cover;
 
</script>
Great that javascript and php can work in sync!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Variable in Javascript

Post by AbraCadaver »

If everything else was OK, then this WILL work:

Code: Select all

<?php echo $article['series']; ?>
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply