|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Member
Join Date: Jun 2006
Posts: 12
|
Help needed with php plus javascript document.write
Hi everyone. I'm getting confused with the syntax when php and javascript is put together on one page. I wonder if someone can help with this.
I have a file called pull.php with the following contents. <script src="source.php" type="text/JavaScript"></script> Then I have a file called source.php with the following contents: <? $adid=$_REQUEST ['adid']; include ("db.php"); $query = "SELECT * FROM ads"; $columns = 2; $result = mysql_query($query); //we add this line because we need to know the number of rows $num_rows = mysql_num_rows($result); echo "<TABLE BORDER=\"1\">\n"; //changed this to a for loop so we can use the number of rows for($i = 0; $i < $num_rows; $i++) { $row = mysql_fetch_array($result); if($i % $columns == 0) { //if there is no remainder, we want to start a new row echo "<TR>\n"; } echo "<TD width='100' valign='top' height='100'><span class='adHeadline'>". $row['ad_url'] . "</span><br /> <span class='adText'>". $row['ad_text'] ."</span></TD>\n"; if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) { //if there is a remainder of 1, end the row //or if there is nothing left in our result set, end the row echo "</TR>\n"; } } echo "</TABLE>\n"; ?> My problem is that I need to insert the javascript's document.write somewhere above but I'm not sure where or how to do it as the syntax is baffling me. The end result I want to achive is that if you call pull.php, the outputted html from source.php must display on pull.php. Everything I have tried has either displayed nothing on pull.php (even if source.php parses correctly) or I get a host of parse errors in source.php. Can anybody help please? Regards Anwaraa P/S: The php code above parses correctly as is. |
|
|
|
|
|
#2 |
|
Follow Me On Twitter: @djg
![]() ![]() ![]() ![]() ![]() Join Date: Aug 2000
Location: Philadephia, PA
Posts: 19,741
|
At the top of source.php:
PHP Code:
PHP Code:
|
|
|
|
|
|
#3 |
|
SitePoint Member
Join Date: Jun 2006
Posts: 12
|
Hi Dan,
Thanks for a quick reply. I have tried your recommendation but I get a blank when I pull up pull.php. Any other ideas? Best regards Anwaraa |
|
|
|
|
|
#4 |
|
Follow Me On Twitter: @djg
![]() ![]() ![]() ![]() ![]() Join Date: Aug 2000
Location: Philadephia, PA
Posts: 19,741
|
Pull up source.php directly and look at the output. Make sure it is what you expect, and is valid JavaScript. Look for JavaScript errors (lower left corner in IE, yellow icon... JavaScript console in Firefox).
|
|
|
|
|
|
#5 |
|
SitePoint Member
Join Date: Jun 2006
Posts: 12
|
Hi Dan
I think I see the problem. FYI the source.php echos the actual table and not the html for the table. Maybe I should delete the echo of the table from source.php and try. Any bettter ideas? Thanks for all your kind help so far. Regards Anwaraa |
|
|
|
|
|
#6 | |
|
Follow Me On Twitter: @djg
![]() ![]() ![]() ![]() ![]() Join Date: Aug 2000
Location: Philadephia, PA
Posts: 19,741
|
Quote:
|
|
|
|
|
|
|
#7 |
|
SitePoint Member
Join Date: Jun 2006
Posts: 12
|
Hi Dan
I get the following when I view the source: document.write('<TABLE BORDER="1"> <TR> <TD width='100' valign='top' height='100'><span class='adHeadline'>http://www.thehomeworker.org</span><br /> <span class='adText'>Make money by working at home. Free info.</span></TD> <TD width='100' valign='top' height='100'><span class='adHeadline'>http://www.thehomeworker.org</span><br /> <span class='adText'>Make money at home. Start now.</span></TD> </TR> <TR> <TD width='100' valign='top' height='100'><span class='adHeadline'><a href='http://www.dom.com'>Frees ads</a></span><br /> <span class='adText'>get free exposure</span></TD> </TR> </TABLE> ') |
|
|
|
|
|
#8 |
|
Follow Me On Twitter: @djg
![]() ![]() ![]() ![]() ![]() Join Date: Aug 2000
Location: Philadephia, PA
Posts: 19,741
|
First JS error that pops out is that the entire thing is enclosed in single quotes but there are also single quotes within the string. Since the only double quotes inside the string are the border="1" in the first tag, what I'd do is.. switch the document.write() to use double quotes around the entire string of HTML, and change the double quotes in the TABLE BORDER="1" to single quotes.
|
|
|
|
|
|
#9 |
|
SitePoint Member
Join Date: Jun 2006
Posts: 12
|
Hi Dan
Problem fixed. I'm very new to javascript but it seems that the there shouldn't be a space between table td and tr tags like so: document.write("<TABLE BORDER='1'><TR><TD width='100' valign='top' height='100'><span class='adHeadline'>http://www.thehomeworker.org</span><br /> <span class='adText'>Make money by working at home. Free info.</span></TD><TD width='100' valign='top' height='100'><span class='adHeadline'>http://www.thehomeworker.org</span><br /><span class='adText'>Make money at home. Start now.</span></TD></TR><TR><TD width='100' valign='top' height='100'><span class='adHeadline'><a href='http://www.dom.com'>Frees ads</a></span><br /><span class='adText'>get free exposure</span></TD></TR></TABLE>") Regards Anwaraa |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 19:52.












Linear Mode
