r-it Posted August 19, 2008 Share Posted August 19, 2008 I am having a problem with one of my scripts, here is the code, and the explanation below it: <script language="Javascript" type="text/javascript"> var i = 0; var timerID = 0; var SlideText = new Array(); <?php $j = 0; $sql = $conn->query("SELECT * FROM faq"); $nr = mysql_num_rows($sql); while($a = $conn->fetchArray($sql)) { $faqQ = $a['faqQuest']; $faqA = $a['faqAns']; $disp .= "SlideText[$j] = \"$faqQ<br />$faqA\";\n"; $j++; } echo $disp; ?> function nextslide() { if(timerID) { clearTimeout(timerID); i++; } var text = document.getElementById("faqtext"); text.innerHTML = SlideText[i]; if(i == 3) { i = -1; } timerID = setTimeout("nextslide()",4000); } </script> Basically what is supposed to happen is that the content is read from the faq table, populates the $disp variable and then echo it, which will create the array text. This text is to be shown in a div called faqtext and at the moment when i view the source, everything is as it should be, the only problem is that the text is not showing in the div, please help. Link to comment https://forums.phpfreaks.com/topic/120338-solved-php-and-javascript/ Share on other sites More sharing options...
adam291086 Posted August 19, 2008 Share Posted August 19, 2008 Javascript and php dont work well together. You may want to look into ajax. Look in this forum under ajax. One of the main topics has a little tutorial that is easy to adapt to your needs. Link to comment https://forums.phpfreaks.com/topic/120338-solved-php-and-javascript/#findComment-620056 Share on other sites More sharing options...
r-it Posted August 19, 2008 Author Share Posted August 19, 2008 I'm sure i can manage that stuff with ajax, but the thing is i have used such a script, albeit without the php and database and it works fine, i would like to know why the stuff that is being echoed is not working. Could it be that the way javascript reads the page is by first going through the javascript stuff and then going back to the php, that is what i need to understand. Link to comment https://forums.phpfreaks.com/topic/120338-solved-php-and-javascript/#findComment-620067 Share on other sites More sharing options...
adam291086 Posted August 19, 2008 Share Posted August 19, 2008 opposite way around. The php will happen before the page is loaded. Hence the reason why when you look at the source code the results from the php are there and not the php code itself. As you are putting the php inside the script tags the browser is treating it like script and therefore not showing the page on the screen but within the code source. If you move the php out of the script tags then it will be displayed. Link to comment https://forums.phpfreaks.com/topic/120338-solved-php-and-javascript/#findComment-620073 Share on other sites More sharing options...
obsidian Posted August 19, 2008 Share Posted August 19, 2008 There are really a couple things that may be happening, but most likely, it is an issue with quotes or some other character interfering with your JavaScript code when your PHP spits it out. Try cleaning up your code some with this type of thing: Change this: <?php $disp .= "SlideText[$j] = \"$faqQ<br />$faqA\";\n"; ?> To this: <?php $disp .= "SlideText.push(\"" . htmlentities($faqQ, ENT_QUOTES) . "<br />" . htmlentities($faqA, ENT_QUOTES) . "\");\n"; ?> Also, it would be helpful to debug if you could provide us the text that is actually getting echoed out, too. Link to comment https://forums.phpfreaks.com/topic/120338-solved-php-and-javascript/#findComment-620077 Share on other sites More sharing options...
obsidian Posted August 19, 2008 Share Posted August 19, 2008 opposite way around. The php will happen before the page is loaded. Hence the reason why when you look at the source code the results from the php are there and not the php code itself. As you are putting the php inside the script tags the browser is treating it like script and therefore not showing the page on the screen but within the code source. If you move the php out of the script tags then it will be displayed. For this type of thing, AJAX is really overkill. He is simply trying to generate his JavaScript with PHP, and conceptually, he has it right. Let's try to help with the syntax and where the error is actually occurring in the code. I think that if we can see the source code of the rendered page, we may be able to assist in a better way. Out of curiosity, are there any JavaScript errors being thrown if you view the page in FF with the Firebug plugin? Link to comment https://forums.phpfreaks.com/topic/120338-solved-php-and-javascript/#findComment-620081 Share on other sites More sharing options...
r-it Posted August 20, 2008 Author Share Posted August 20, 2008 guys thanks a lot, it was my stupidity that caused all this. I almost resorted to using ajax which i really did not need for a simple task such as this. The problem was that i wasnt initialising the javascript function in the body onload. I was really giving up as firebug was not giving me any errors whatsoever, thanks again guys for the effort. Link to comment https://forums.phpfreaks.com/topic/120338-solved-php-and-javascript/#findComment-621008 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.