SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    At the top of source.php:

    PHP Code:
    echo "document.write("
    And at the end
    PHP Code:
    echo ");"

  3. #3
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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. #5
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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. #9
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •