SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict -Ice-php's Avatar
    Join Date
    May 2002
    Location
    UK
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL and Javascript

    Hey
    i have a PHP script but ive been told what i want to acomplish cant be done in PHP, but it can in Javascript.

    when a user selects another user from a dropdown menu i want a separate field (NON-EDITABLE) to show some information from a variable which holds MySQL Info

    Did you understand this?
    Anyone give me a bit of code to look at?
    Also because i dont actually know java script could you maybe add a little comment of what does what
    although some JS i have seen has some V similar syntax as PHP
    Thanks !
    -Ice

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, javascript and PHP share a lot of syntax. Think of javascript as an event-driven scripting language. Some event occurs (pageload, mouseover, option select) and then something happens (usu. a function call)

    First, you will need to get PHP to echo an array of data that will be associated with the SELECT object. Since SELECT object's OPTIONs are arrays, it will be convenient to just create an array. Javascript supports literal notations for arrays. Example

    PHP Code:
    echo( '<script type="text/javascript">\n' );
    echo( 
    'var descData = new Array();\n' );
    $i 0;
    $result mysql_query"SELECT description FROM users" );
    while ( 
    $row mysql_fetch_array$resultMYSQL_ASSOC ) )
    {
        echo( 
    'descData[$i] = $row[description];\n' );
        
    $i++;
    }
    echo( 
    '</script>' ); 
    The above should output (assuming 5 rows retrieved) will print
    Code:
    <script type="text/javascript">
    var descData = new Array();
    descData[0] = 'description1';
    descData[1] = 'description2';
    descData[2] = 'description3';
    descData[3] = 'description4';
    descData[4] = 'description5';
    </script>
    Now, our HTML page
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function setDescription( i )
    {
        document.getElementById('description').innerText = descData[i];
    }
    </script>
    
    <!-- output from above here -->
    
    </head>
    <body>
    
    <select onchange="setDescription( this.selectedIndex )">
        <option>One</option>
        <option>Two</option>
        <option>Three</option>
        <option>Four</option>
        <option>Five</option>
    </select>
    
    <div id="description"></div>
    
    </body>
    </html>
    And that's it!
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •