Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Jun 19, 2006, 06:54   #1
anwaraa
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.
anwaraa is offline   Reply With Quote
Old Jun 19, 2006, 07:05   #2
Dan Grossman
Follow Me On Twitter: @djg
gold trophysilver trophybronze trophy
SitePoint Award Recipient
 
Dan Grossman's Avatar
 
Join Date: Aug 2000
Location: Philadephia, PA
Posts: 19,741
At the top of source.php:

PHP Code:

echo "document.write("; 

And at the end
PHP Code:

echo ");"; 

Dan Grossman is online now   Reply With Quote
Old Jun 19, 2006, 08:00   #3
anwaraa
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
anwaraa is offline   Reply With Quote
Old Jun 19, 2006, 09:00   #4
Dan Grossman
Follow Me On Twitter: @djg
gold trophysilver trophybronze trophy
SitePoint Award Recipient
 
Dan Grossman's Avatar
 
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).
Dan Grossman is online now   Reply With Quote
Old Jun 19, 2006, 09:30   #5
anwaraa
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
anwaraa is offline   Reply With Quote
Old Jun 19, 2006, 10:10   #6
Dan Grossman
Follow Me On Twitter: @djg
gold trophysilver trophybronze trophy
SitePoint Award Recipient
 
Dan Grossman's Avatar
 
Join Date: Aug 2000
Location: Philadephia, PA
Posts: 19,741
Quote:
Originally Posted by anwaraa
FYI the source.php echos the actual table and not the html for the table.
That doesn't make sense. The browser doesn't know what a table is unless it's been sent the HTML to display one. What I wanted you to do was point your browser to http://www.example.com/source.php then view the source of that page to see what the PHP script was outputting, so you can verify it's valid javascript.
Dan Grossman is online now   Reply With Quote
Old Jun 19, 2006, 10:27   #7
anwaraa
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>
')
anwaraa is offline   Reply With Quote
Old Jun 19, 2006, 10:35   #8
Dan Grossman
Follow Me On Twitter: @djg
gold trophysilver trophybronze trophy
SitePoint Award Recipient
 
Dan Grossman's Avatar
 
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.
Dan Grossman is online now   Reply With Quote
Old Jun 19, 2006, 11:06   #9
anwaraa
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
anwaraa is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 19:52.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved