Jump to content

Using Javascript With Php


kensbeijing

Recommended Posts

Ok quite simply this is what I have

<div id="box"><?php echo $a1; ?></div><script language="JavaScript">function change() {	document.getElementById('box').innerHTML = "<?php echo $a2;?>";}</script>

When I call the js function change(), the html inside the div should change so it echos the php variable $a2. But it's not happening, the page doens't load up properly,and I'm guessing it's because I can't mix the php in with the js. How would I change the echoed variable, without using all that fancy AJAX stuff?Variables $a1 and $a2 are pretty big.

Link to comment
Share on other sites

View Source in the browser to see what that part of the script actually looks like. Your built-in Firefox error console (or Firebug if you use it) should give you more information.Anyone wanting to use the error console should know that it clutters up VERY QUICKLY, because most pages on the web are full of errors. The most recent (ie, the page you're working on) are at the bottom, so you have to scroll to find them. I personally clear the console (there's a button at the top) almost every time I refresh a page under development.

Link to comment
Share on other sites

Thanks, but I figured out why. My variable was very big, and included many html tags inside, and for some reason it doesn't work with innerHTML. I tried smaller variables with a few words and that worked fine. So my new solution is to use CSS to make the visibility of $a2 hidden, and when the function is called, $a1 becomes hidden and in turn makes $a2 visible. This is a much nicer way, especially in IE, when changing the innerHTML, the text becomes fuzzy.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...