0

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>
5
  • If it's not populated from DB - how this question is related to HTML and javascript? Commented Sep 12, 2012 at 4:39
  • 1
    Please show the code where $ba is being declared. Is it inside of a function? Also, your javascript should be document.getElementById(ba); Commented Sep 12, 2012 at 4:44
  • removed quotes from getElementById(ba) did'nt make any differance Commented Sep 12, 2012 at 8:39
  • @user1664457 what's the actual value of $ba when it fails Commented Sep 12, 2012 at 17:34
  • if $ba has a value of 127 and is set from a mysql_query it fails if I manualy set the value to 127 using $ba=127; or $ba="127"; right before the fill_div() call it works Commented Sep 13, 2012 at 0:47

4 Answers 4

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
  • I added stripslashes on the off chance it may make a difference and the " have now been removed with same results Commented Sep 12, 2012 at 8:42
3

try ..

 document.getElementById(ba).innerHTML
1
document.getElementById("ba")

should be

document.getElementById(ba)
0

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.

1
  • thanks for your answer but even without the " still not working Commented Sep 12, 2012 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.