My problem is I have a php variable $ba with a value of 147 retrieved from a DB the below snipets of my code does not work, no image is shown. If I add $ba=147; before the fill_div call it works perfectly. I am stumped as to why it dos'nt work when $ba populated from the DB. In both cases a check of the source code for the page shows the call being filled correctly fill_div("147");
<script type='text/javascript'>
function fill_div(ba)
{
document.getElementById("ba").innerHTML="<img src='admin/images/image.gif'/>";
}
</script>
<script>
fill_div("<? echo stripslashes($ba); ?>");
</script
<div id="<? echo $ba ?>" style="border:1px solid; width:120px; height:40px"></div>
4 Answers 4
You're passing the string literal"ba" to getElementById when you need to pass the variable ba
document.getElementById(ba).innerHTML="<img src='admin/images/image.gif'/>";
Also the stripslashes might not be a good idea if $ba has quotes in it, if $ba has a " in it will cause an error in your JavaScript if it is not escaped.
1 Comment
try ..
document.getElementById(ba).innerHTML
Comments
document.getElementById("ba")
should be
document.getElementById(ba)
Comments
Try changing your document.getElementById to:
document.getElementById(ba)
If it has quotes (as in your example code) "ba" is treated as a string, not a variable.
$bais being declared. Is it inside of a function? Also, your javascript should bedocument.getElementById(ba);$bawhen it fails