Please help with small script creation.

Discussion in 'JavaScript' started by malc, Jul 5, 2006.

  1. #1
    I would like my users to be able to use small parts of of my sites pages on their site.
    For example: An HTML Table with statistics. Users then insert a line of javascript in their site and my table will display.

    Can anyone tell me how to do this?
     
    malc, Jul 5, 2006 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,807
    Likes Received:
    4,534
    Best Answers:
    123
    Trophy Points:
    665
    #2
    You just need to create a script on your site which might be called with

    <script src='http://www.mysite.com/data.php' type='text/javascript'></script>
    Code (markup):
    data.php will actually output valid javascript but will be created by PHP on the server and will have document.write() type commands to output the correct data.

    It's not hard once you get your head around it.
     
    sarahk, Jul 5, 2006 IP
  3. malc

    malc Peon

    Messages:
    129
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you sarahk,
    I think I almost understand.
    Don't know how to implement document.write() but I'll do a search.
     
    malc, Jul 5, 2006 IP
  4. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #4

    you use php to do all necessary database connections, information gathering, calculations, etc and you then echo the information with document.write(). This is necessary because when the php script outputs the information to the javascript, the javascript won't know what to do with the it so you use "document.write()" to tell it to write it to the page.

    This is a sample php file (I called it js.php):
    <?php
    $number = 1+2;
    $name= "three";
    
    echo "document.write('You are using information from mypage.com! visit us now');";
    echo "document.write('<br />');";
    echo "document.write('number $number is written:$name');";
    
    ?>
    PHP:
    This is the html page:

    <html>
    
    	<head>
    		<title></title>
    	</head>
    	<body>
    <script src='http://localhost:8888/js.php' type='text/javascript'></script>
    
    	</body>
    </html>
    
    HTML:
    And this is what the html page will output to the user:

    You are using information from mypage.com! visit us now
    number 3 is written:three
     
    danielbruzual, Jul 5, 2006 IP
  5. malc

    malc Peon

    Messages:
    129
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you very much for the advice danielbruzual. :D
     
    malc, Jul 6, 2006 IP