SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Autofill Text Field

    Hi all,
    I am unable to get the relevant Database data to display in the textfields.

    the data is pulled from a database. And based on the users selection from a select menu each textfield should be automatically populated with the relevant data.

    Any ideas?? Thanks.

    PHP Code:
     <?php
             
     
    // 1) Get all Investment Types
     
    $query "SELECT investment_type_ID, investment_type, inv_note, inv_note2 ".
             
    "FROM investment_type";
     
    $result mssql_query($query) or die('Select Error');
     
     while(
    $row mssql_fetch_array($result))
     {
             
    // Create Arrays + Populate from DB
             
    $id_array[] = $row['investment_type_ID'];
             
    $type_array[] = $row['inv_note'];
             
    $type1_array[] = $row['inv_note2'];
             
    $investment_type[] = $row['investment_type'];                    
     
     }
    // end while
             
     // Loop through $id_array to populate select menu
     
    for($i=0$i<sizeof($id_array); $i++)
     {
             
    $option .= '<option value="'.$id_array[$i].'">'.$investment_type[$i].'</option>';    
     }
     
    ?>
             
     <script language="Javascript">
                             
                 // Declare Javascript Arrays
                 var js_id_array = new Array();
                 var js_type_array = new Array();
                 var js_type1_array = new Array(); 
             
     </script>
             
     <?php
             
             
    // Loop through PHP Arrays
             
    for($i=0$i <sizeof($id_array); $i++)
             {
     
    ?>
     
     <script language="Javascript">
             
         // Assign PHP Arrays to Javascript Arrays
         var js_id_array['<?php echo $i ?>'] = '<?php echo $id_array[$i?>';
         var js_type_array['<?php echo $i ?>'] = '<?php echo $type_array[$i?>';
         var js_type1_array['<?php echo $i ?>'] = '<?php echo $type1_array[$i?>';
             
     </script>
     
     <?php
         
    // end for loop
     
    ?>
         
     <script language="Javascript">
     
     function change(id)
     {
        document.getElementById('inv_type_note').innerHTML = js_type_array[id];
        document.getElementById('inv_type_note2').innerHTML = js_type1_array[id];
     }
     </script>
     
         
         <p>
           <label for="investment_type">Investment Type</label>
     <select name="investment_type" id="investment_type" onchange="change(this.options[selectedIndex].value);">
               <option value="0">Select Investment Type</option>
               <?php
                          
    echo $option;
               
    ?>
                 </select>
         </p>
             
         <p>
               <label for="inv_type_note">Note</label>
           <textarea cols="55" rows="5" name="inv_type_note" id="inv_type_note">
           </textarea>
         </p>
     
         <p>
     <label for="inv_type_note2">Note</label>
     <textarea cols="45" rows="8" name="inv_type_note2" id="inv_type_note2"></textarea>
         </p>

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This is generally something done with AJAX, so as to reduce the need to set all those array variables, and turn it into one request. I highly recommend the sitepoint article Take Command with AJAX to get you started on that.

  3. #3
    SitePoint Enthusiast blindpete's Avatar
    Join Date
    Dec 2003
    Location
    Fairfax VA, USA
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found the my-bic AJAX library to be very usefull for exactly this type of task. It is very easy to implment. The site includes many tutorials. http://www.litfuel.net/mybic/index.html?page=features

    What I like about this library is its small, its just AJAX, its simple to use.
    -Pete


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
  •