Results 1 to 3 of 3

Thread: Adding PHP to javascript

  1. #1
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding PHP to javascript

    Hey,

    I have a news scroller, and I would like the content taken from a database...

    Currently the content is set using a js page containing:

    var pausecontent=new Array()
    pausecontent[0]='<a href="http://www.javascriptkit.com">JavaScript Kit</a><br />Comprehensive JavaScript tutorials and over 400+ free scripts!'
    pausecontent[1]='<a href="http://www.codingforums.com">Coding Forums</a><br />Web coding and development forums.'
    pausecontent[2]='<a href="http://www.cssdrive.com" target="_new">CSS Drive</a><br />Categorized CSS gallery and examples.'

    Is there a way to replace this javascript with the content from a database?

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    PHP Code:
    <?php 
    $increase 
    0;
    $increase2 0;
                while (
    sqlsrv_fetch$stmt )) {
                 
    $differentvalue[]= sqlsrv_get_field$stmt0SQLSRV_PHPTYPE_STRINGSQLSRV_ENC_CHAR));
                    
    $value[] = sqlsrv_get_field$stmt1);
                    
    $anothervalue[]= sqlsrv_get_field$stmt2);
                    
    $increase++;
                    }
    echo 
    "<script type=\"text/javascript\">\nvar ARRAY_NAME=new Array();\n";
        while ( 
    $increase2 $increase) {
        echo 
    "ARRAY_NAME[$increase2] = $value[$increase2];\n";
        
    $increase2++;
        }
        
    ?>
    I think this would do it this is for a microsoft sql server though might need some alerting if different

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    PHP just writes text: PHP will generate html and javascript TEXT, but not be actually interactive with the javascript when it's active on the page. So you can use PHP to create database-stored content, but it will not reload once the page is active/loaded.
    The above post gives a way to approach the problem, but remember that javascript and PHP do not communicate directly. You can use Ajax (a way of requesting a PHP script live through javascript) if you must have "live" php on the page.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •