www.webdeveloper.com
Results 1 to 7 of 7

Thread: help me with this javascript code

  1. #1
    Join Date
    Jun 2013
    Posts
    33

    help me with this javascript code

    I wanted a dropdown box which will display different tables fetched from mysql after selecting options.
    so i have this code.

    But problem here is when page loads it displays all tables. (It should only display dropdown box.)
    and when i choose options from downdown it works well.

    what is the problem? pls help thanks.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function changeTblDisplay(dhi) { 
      if (dhi == '') { return ; }
      var lee = document.getElementById('statTables').getElementsByTagName('div');
      for (var i=0; i<lee.length; i++) { lee[i].style.display = 'none'; }
      document.getElementById(dhi).style.display = 'block';  
    }
    </script>
    </head>
    <body>
    
    <div class="styleit">
    <select id="tables" onchange="changeTblDisplay(this.value)">
      <option value="">Choose Table </option>
      <option value="table1"> A </option>
      <option value="table2"> B </option>
      <option value="table3"> C </option>
    
    <option value="table4"> D </option>
    <option value="table5"> E </option>
    <option value="table6"> F </option>
    <option value="table7"> G </option> 
    </select>
    </div>
    
    <?php
    
    $host = "localhost";
    
    $user = "root";
    
    $pass = "d";
    
    $db = "test"; 
    
    $conne = mysql_connect($host,$user,$pass) or die("couldnot connect");
    
    mysql_select_db($db)or die("unable to connect");
    
    ?>
    <div id="statTables">
    
    <div id="table1" >
     <table border="1" >
     <thead> <caption> Table 1 </caption> </thead>
    <?php
    
    $query = "SELECT * FROM mytable where ram = 'a'";
    
    $result = mysql_query($query);
    if(mysql_num_rows($result)>0)
    {
    	//print rows   
     
                    while($row = mysql_fetch_row($result))
                     {
                                     echo "<tr>";
                                      
                                     echo "<td>".$row[1]."</td>";
                                     echo "<td>".$row[2]."</td>";
                                     echo "<td>".$row[3]."</td>";
                                     echo "<td>".$row[4]."</td>";
     
                                     echo "</tr>";
                                                
                     }
                     echo "</table>";
    }
    else {echo "no result found";}
    ?>
    </div>
    
    <div id="table2" >
     <table border="1" >
     <thead> <caption> Table 2 </caption> </thead>
    <?php
    
    $query = "SELECT * FROM mytable where ram = 'b'";
    
    $result = mysql_query($query);
    if(mysql_num_rows($result)>0)
    {
    	//print rows   
     
                    while($row = mysql_fetch_row($result))
                     {
                                     echo "<tr>";
                                      
                                     echo "<td>".$row[1]."</td>";
                                     echo "<td>".$row[2]."</td>";
                                     echo "<td>".$row[3]."</td>";
                                     echo "<td>".$row[4]."</td>";
     
                                     echo "</tr>";
                                                
                     }
                     echo "</table>";
    }
    else {echo "no result found";}
    ?>
    </div>
    
    <div id="table3" >
     <table border="1" >
     <thead> <caption> Table3 </caption> </thead>
    <?php
    
    $query = "SELECT * FROM mytable where ram = 'c'";
    
    $result = mysql_query($query);
    if(mysql_num_rows($result)>0)
    {
    	//print rows   
     
                    while($row = mysql_fetch_row($result))
                     {
                                     echo "<tr>";
                                      
                                     echo "<td>".$row[1]."</td>";
                                     echo "<td>".$row[2]."</td>";
                                     echo "<td>".$row[3]."</td>";
                                     echo "<td>".$row[4]."</td>";
     
                                     echo "</tr>";
                                                
                     }
                     echo "</table>";
    }
    else {echo "no result found";}
    ?>
    </div>
    
    <div id="table4" >
     <table border="1" >
     <thead> <caption> table4 </caption> </thead>
    <?php
    
    $query = "SELECT * FROM mytable where ram = 'd'";
    
    $result = mysql_query($query);
    if(mysql_num_rows($result)>0)
    {
    	//print rows   
     
                    while($row = mysql_fetch_row($result))
                     {
                                     echo "<tr>";
                                      
                                     echo "<td>".$row[1]."</td>";
                                     echo "<td>".$row[2]."</td>";
                                     echo "<td>".$row[3]."</td>";
                                     echo "<td>".$row[4]."</td>";
     
                                     echo "</tr>";
                                                
                     }
                     echo "</table>";
    }
    else {echo "no result found";}
    ?>
    </div>
    
    <div id="table5" >
     <table border="1" >
     <thead> <caption> Table5 </caption> </thead>
    <?php
    
    $query = "SELECT * FROM mytable where ram = 'e'";
    
    $result = mysql_query($query);
    if(mysql_num_rows($result)>0)
    {
    	//print rows   
     
                    while($row = mysql_fetch_row($result))
                     {
                                     echo "<tr>";
                                      
                                     echo "<td>".$row[1]."</td>";
                                     echo "<td>".$row[2]."</td>";
                                     echo "<td>".$row[3]."</td>";
                                     echo "<td>".$row[4]."</td>";
     
                                     echo "</tr>";
                                                
                     }
                     echo "</table>";
    }
    else {echo "no result found";}
    ?>
    </div>
    
    <div id="table6" >
     <table border="1" >
     <thead> <caption> Table6 </caption> </thead>
    <?php
    
    $query = "SELECT * FROM mytable where ram = 'f'";
    
    $result = mysql_query($query);
    if(mysql_num_rows($result)>0)
    {
    	//print rows   
     
                    while($row = mysql_fetch_row($result))
                     {
                                     echo "<tr>";
                                      
                                     echo "<td>".$row[1]."</td>";
                                     echo "<td>".$row[2]."</td>";
                                     echo "<td>".$row[3]."</td>";
                                     echo "<td>".$row[4]."</td>";
     
                                     echo "</tr>";
                                                
                     }
                     echo "</table>";
    }
    else {echo "no result found";}
    ?>
    </div>
    
    <div id="table7" >
     <table border="1" >
     <thead> <caption>Table7 </caption> </thead>
    <?php
    
    $query = "SELECT * FROM mytable where ram = 'g'";
    
    $result = mysql_query($query);
    if(mysql_num_rows($result)>0)
    {
    	//print rows   
     
                    while($row = mysql_fetch_row($result))
                     {
                                     echo "<tr>";
                                      
                                     echo "<td>".$row[1]."</td>";
                                     echo "<td>".$row[2]."</td>";
                                     echo "<td>".$row[3]."</td>";
                                     echo "<td>".$row[4]."</td>";
     
                                     echo "</tr>";
                                                
                     }
                     echo "</table>";
    }
    else {echo "no result found";}
    ?>
    </div>
    
    </div>
    </body>
    </html>

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    try this

    Code:
    <style>
    #statTables div{display:none;}
    </style>
    <script type="text/javascript">
    function changeTblDisplay(dhi){
    if (dhi == ''){return;}
    var lee = document.getElementById('statTables').getElementsByTagName('div');
    for (var i=0; i<lee.length; i++) { lee[i].style.display = 'none';}
    document.getElementById(dhi).style.display = 'block';  
    }
    window.onload=function(){changeTblDisplay(here_goes_the_id_you_need_to_show);}
    </script>
    </head>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Jun 2013
    Posts
    33
    Thanks Padonak, thats exactly what i needed.

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    you're welcome
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  5. #5
    Join Date
    Jun 2013
    Posts
    33
    well, how to do this using Jquery?

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    Code:
    <style>
    #statTables div{display:none;}
    </style>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript">
    function changeTblDisplay(dhi){
    if(dhi==''){return;}
    $('#statTables div').css('display','none');
    $('#'+dhi).css('display','block');
    }
    $(document).ready(function(){changeTblDisplay(here_goes_the_id_you_need_to_show);});
    </script>
    </head>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  7. #7
    Join Date
    Jun 2013
    Posts
    33
    Thanks padonak,

    Code:
    $(".selection td:first-child + td").click(function(){
              });
    in this code i select the second child of <td>....Now i want the first child of the <td> which user clicked.

    i did this..

    Code:
    $(".selection td:first-child + td").click(function(){
    var first_child_of_selected_column = $(".selection td:first-child").text();
    });
    i also tried "this" but couldnt get the result.
    pls help.thnks

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Posting Permissions

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



Recent Articles