Results 1 to 6 of 6

Thread: [RESOLVED] Get HTML source code using JavaScript

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    Resolved [RESOLVED] Get HTML source code using JavaScript

    I need to be able to access some database information without directly using PHP. My PHP page on the server will get the information like this...

    <? php
    ...
    $SQL = "SELECT .. FROM .. WHERE id = '" . $id . "'";
    ...
    (create array from SQL for chosen record)
    ...
    $mytext = $sqlResult['somefield'];
    Print($mytext);
    ?>

    So if I go to the page, using pagename.php?id=34 for example, the text for that entry in the database will be displayed in the browser.

    Next, I need to use a page on a different server, that doesn't have PHP running, to access that. So something like...

    <HTML>
    <body>
    <b><script>some javascript to get source code from 'pagename.php?id=34' page and write to document</script></b>
    </body>
    </HTML>

    The purpose of this is for a content generator that stores information in a database but people with webpages on their own servers can include data.

    Any ideas? Thanks in advance.
    Last edited by snappel; Nov 10th, 2005 at 05:06 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Get HTML source code using JavaScript

    You can use XMLHTTP

    Code:
    <script language=javascript> 
        var url = "http://www.mendhak.com/main.php"; 
        var xmlhttp = new ActiveXObject("MSXML2.ServerXMLHTTP"); 
        xmlhttp.open("GET", url, 0); 
        xmlhttp.send(""); 
        document.write(xmlhttp.responseText); 
        var xmlhttp = null; 
    </script>
    HTH

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    Re: Get HTML source code using JavaScript

    Thanks. I now have this code...


    <HTML>
    <head>

    <script language=javascript>

    function getcontent(acc, ind, key) {

    var url = "http://www.mendhak.com/main.php";
    var xmlhttp = new ActiveXObject("MSXML2.ServerXMLHTTP");
    xmlhttp.open("GET", url, 0);
    xmlhttp.send("");
    var xmlhttp = null;
    document.write(xmlhttp.responseText);
    }

    </script>

    </head>
    <body>
    Hello

    <b><a href="javascript:getcontent(0,0,'tyhfx')">get content</a></b>

    </body>
    </HTML>



    But it doesn't work. I click on the link (the parameters can be ignored for now - I'm testing without the PHP page first) and then I get a message saying 'Errors on page' in the status bar. Also, is there any way of making it work without it displaying a dialog box warning that an ActiveX component is about to be used?

    Thanks again.

  4. #4
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    Re: Get HTML source code using JavaScript

    You could use an iframe in the pages served from the other servers or you could run a cgi script on the remote servers to retrieve and display the information. I'd probably go with an iframe as that would be the easiest for novices to implement provided that they would simply be displaying, and not modyfing, any information. They would just add the iframe tag and source it to your page.
    What bug? That's not a bug. It's an undocumented feature!

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    Re: Get HTML source code using JavaScript

    That's a good idea, but for this example I need to just get the text from the database field - it has to fit in with the formatting of the page, etc. There must be a way of doing it without using ActiveX?

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    Re: Get HTML source code using JavaScript

    Ok, this did the trick...

    PHP page...

    <?php
    header("Content-type: text/javascript");

    $mytext = $sqlResult['somefield'];
    Print("document.write(\"".$mytext."\")");
    ?>

    HTML page...

    <script src="http://www.yourserver.com/yourphp.php?parms"></script>


    Not my own idea, but it seems obvious now I look at it.

Posting Permissions

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



Click Here to Expand Forum to Full Width