views:

31

answers:

2

Consider, I've got this code in PHP

<?php
if($count==0)
{
?>
<script>
  show_my_div();
</script>
<?php } ?>

the show_my_div() is supposed to show a division tag. But It doesn't work. Please help.

+3  A: 

There's no standard show_my_div function in PHP or JavaScript. You'd have to post the content of that function for anyone to help you with it.

Outputting a div is easy in PHP:

echo "<div>content</div>";

Similarly, it's easy in JavaScript to create a div element and append it to something. Here's raw JavaScript to append a div to the end of the page:

var div = document.createElement('div');
div.innerHTML = "The content of the div";
document.body.appendChild(div);

You can append elements anywhere, not just at the end. For instance, suppose I have a div with the id foo:

<div id="foo"></div>

...and I want to add a paragraph to the end of it:

var p, div;
div = document.getElementById('foo');
if (div) { // (Just being defensive)
    p = document.createElement('p');
    p.innerHTML = "The text of the paragraph";
    div.appendChild(p);
}

These things are made easier if you use a JavaScript library like Prototype, jQuery, Closure, or any of several others to smooth out browser differences and give you some syntactic sugar.

T.J. Crowder
A: 

Assuming show_my_div exists,

have you tried to add type="text/javascript" to your script tag?

Ben
doesn't work that way.
1s2a3n4j5e6e7v
@1s2a3n4j5e6e7v: It's a good idea anyway. Yes, it's the default in all modern browsers (and now officially the default, as of HTML5), but still...
T.J. Crowder
I've had problem running js script that came down to not having the type attribute provided..
Ben