JavaScript and client side scripting.
Moderator: General Moderators
easinewe
Forum Newbie
Posts: 23 Joined: Mon Jan 11, 2010 3:22 pm
Post
by easinewe » Thu Feb 25, 2010 8:54 am
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>
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Thu Feb 25, 2010 9:09 am
For one you're missing the $:
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
Post
by easinewe » Thu Feb 25, 2010 11:01 am
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>
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Thu Feb 25, 2010 11:06 am
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.
kaszu
Forum Regular
Posts: 749 Joined: Wed Jul 19, 2006 7:29 am
Post
by kaszu » Thu Feb 25, 2010 11:50 am
Code: Select all
$article['series'] = 'chicago'; //quotes?
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Thu Feb 25, 2010 11:56 am
kaszu wrote: Code: Select all
$article['series'] = 'chicago'; //quotes?
Yes, other than that
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
Post
by easinewe » Thu Feb 25, 2010 12:02 pm
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!
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Thu Feb 25, 2010 12:22 pm
If everything else was OK, then this WILL work:
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.