Page 1 of 1

PHP Variable in Javascript

Posted: Thu Feb 25, 2010 8:54 am
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>

Re: PHP Variable in Javascript

Posted: Thu Feb 25, 2010 9:09 am
by AbraCadaver
For one you're missing the $:

Code: Select all

<?php echo $article['series']; ?>

Re: PHP Variable in Javascript

Posted: Thu Feb 25, 2010 11:01 am
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>

Re: PHP Variable in Javascript

Posted: Thu Feb 25, 2010 11:06 am
by AbraCadaver
Looks fine. What does it output?

Re: PHP Variable in Javascript

Posted: Thu Feb 25, 2010 11:50 am
by kaszu

Code: Select all

$article['series'] = 'chicago';  //quotes?

Re: PHP Variable in Javascript

Posted: Thu Feb 25, 2010 11:56 am
by AbraCadaver
kaszu wrote:

Code: Select all

$article['series'] = 'chicago';  //quotes?
Yes, other than that :oops:

Re: PHP Variable in Javascript

Posted: Thu Feb 25, 2010 12:02 pm
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!

Re: PHP Variable in Javascript

Posted: Thu Feb 25, 2010 12:22 pm
by AbraCadaver
If everything else was OK, then this WILL work:

Code: Select all

<?php echo $article['series']; ?>