Jump to content

embedding javascript in php


nicecandy

Recommended Posts

i want to change the content of my div tag dynamically using javascript in PHP. I am using the following technqiue but it is not working.

<?php

echo "<script type='text/javascript'>\n";

echo "document.getElementById('mydiv').innerHTML = 'contents of div.......';";

echo "</script>";

 

?>

 

 

Plz guide me how can i do this

 

Thanks,

 

Best Regards,

Nice Candy.

Link to comment
Share on other sites

Bit confusing, since you can use javascript/html/cssinterchangeably in the middle of php files it seems to make no sense to echo out javascript commands, why not just do something like the following:

<?php
php code here.....
?>
<script type='text/javascript'>
document.getElementById('mydiv').innerHTML = 'contents of div.......';
</script>
<?php 

more php code here

?>

Link to comment
Share on other sites

do you echo your javascript before or after the html?  The way you have inserted the javascript, not in a function or anything like that, it is going to run right when the browser processes it, so if you have it before your div has been parsed then you will get an undefined object.

 

And Zurev does have a point, I have to assume that this is just a proof of concept type thing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.