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
https://forums.phpfreaks.com/topic/220634-embedding-javascript-in-php/
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

?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.