dynamic populating of fields based on selection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • angelicdevil
    New Member
    • Apr 2009
    • 116

    dynamic populating of fields based on selection

    ok this is wat i was trying to do...i have a list field with options A,B,P now i want the second list field to show data list by getting them from database based on the selection made in listbox1.

    e.g if i select A in listbox 1 then listbox 2 should display automatically all the data from database where the status is A.
    and as i change the seelction it should display different data accordingly.

    but in my code it doesnt display any data in the listbox2

    plz help

    Code:
    <body>
    
    
    <div style="position: absolute; width: 100px; height: 32px; z-index: 1; left: 225px; top: 5px" id="layer1">
    <td><select name="status" maxlength="1">
    <?php
           
                echo '<option>'.A.'</option>';echo "<BR>";
                 echo '<option>'.P.'</option>';echo "<BR>";
                  echo '<option>'.B.'</option>';echo "<BR>";
                  
    ?> 
     
     
      </select> </td>
      </div>
    <form action="listpopulate.php" method="post">
    <table>
    
    <td><select name="username" maxlength="23">
    <?php
           	$username = 'username';
    	$usernamevalue = array();
    	$status = 'status';      
     
    	$query_name = "SELECT users.username FROM users WHERE users.status='".mysql_real_escape_string($status)."'";
        $result = mysql_query($query_name);
        confirm_query($result);
                while ($record = mysql_fetch_assoc($result)) {
            while (list($username, $usernamevalue) = each ($record)) {
            echo '<option>'.htmlentities($usernamevalue).'</option>';
           
            }
            echo "<BR>";
                   
        }   
                 
       
    ?> 
      </select> </td>
     
               
    </div>
    
    
     </table>
     
    </form>
    </body>
    Last edited by pbmods; Apr 26 '09, 06:08 PM. Reason: Fixed CODE tags.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Your option elements need a value attribute. That way, when you submit the form, you will know the value of the selected option in the $_POST array.

    Form:
    Code:
    <form ... >
      <select name="status">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
      </select>
    </form>
    And your PHP would look something like below:
    Code:
    // If there is no POST data present, we'll set a default value.
    // Read: ternary operator (php.net)
    $status = (isset($_POST['status'])) ? $_POST['status'] : 1;
    
    echo "You selected: {$status}";
    
    // Use $status in SQL.
    $sql = "SELECT * FROM `tbl_name` WHERE `status` = '{$status'}";
    Hope this helps,
    Mark.

    Comment

    • angelicdevil
      New Member
      • Apr 2009
      • 116

      #3
      ok this is what i changed as you said but its not auto populating based on selection

      plz tell me what i need to change on which line nos.

      thanx

      Code:
      <body>
      
      <form action="listpopulate.php" method="post">
      <table>
      <td><select name="status" maxlength="1">
      <?php
             $status_name = 'status';
             $status_value = array();
             $query_status = " SELECT status_type.status from status_type ";
             $result_status = mysql_query($query_status);
              confirm_query($result_status);
             
             // status list box // 
             
             while ($record = mysql_fetch_assoc($result_status)) {
              while (list($status_name, $status_value) = each ($record)) {
              echo '<option value ="'.htmlentities($status_value).'">'.htmlentities($status_value).'</option>';
             
              }
               }
               $status = (isset($_POST['status'])) ? $_POST['status'] : "";
               if ($status == $_POST['status']){
               $c = 1;
               }
               
                    
      ?> 
       
        </select> </td>
       
      
      <td><select name="username" maxlength="23">
      <?php
             	
             	$username = 'username';
      	$usernamevalue = array();
      	  
      	$query_name = "SELECT users.username FROM users WHERE users.status='{$status}'";
              $result = mysql_query($query_name);
              confirm_query($result);
          // users listbox // 
          if ($c ==1){
          if ($result)
      {
              while ($record = mysql_fetch_assoc($result)) {
              while (list($username, $usernamevalue) = each ($record)) {
              echo '<option value ="'.htmlentities($usernamevalue).'">'.htmlentities($usernamevalue).'</option>';
             
              }
              echo "<BR>";
                     
          }   
                   
       } 
       
       } 
      ?> 
        </select> </td>
         </table>
       
      </form>
      </body>

      Comment

      • waqasahmed996
        New Member
        • Jun 2008
        • 160

        #4
        i think you have to apply ajax here

        by using ajax you can easily do this

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Lines 22 - 24 make no sense. The reason for using the ternary operator is to easily set a default value based on a condition.

          Code:
          $status = (isset($_POST['status'])) ? $_POST['status'] : 1;
          Are you wanting the 'auto populating' to be done without the page refreshing?

          Comment

          • angelicdevil
            New Member
            • Apr 2009
            • 116

            #6
            yes without page refreshing or click button

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Then you need to look into, AJAX. There are lots of readily available tutorials if you search google.

              Comment

              • angelicdevil
                New Member
                • Apr 2009
                • 116

                #8
                something like http://www.plus2net.com/php_tutorial/dd.php only they wat their r doing is going over my head . it wud be gr8 if u can help me get same result with my code or how i can make changes in tht code to get my result. since they use javascript n i know nothing abt java

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  Originally posted by angelicdevil
                  since they use javascript n i know nothing abt java
                  Java and Javascript have only the first four letters in common ;). besides that, Javascript is an essential part of AJAX (thus the name).

                  if the javascript is too much for you consider using a Javascript library (like ExtJS, JQuery, Prototype, …) they should provide functions to make AJAX actions easier.

                  Comment

                  • Ciary
                    Recognized Expert New Member
                    • Apr 2009
                    • 247

                    #10
                    one hing to know about PHP: it's only execued at page load. so if you want do do anything with PHP you either need to reload the page or use an AJAX request to a php page with the code.

                    if you really want something to change without reloading the page u need to use javascript. with php this can't be done.

                    a bit of theory: php is a server side language which wil be executed on the server before your browser receives it. thats why you can't see php in your sourcecode. and you just cant execute php without asking the server to reload te page.

                    i hope that helps you understand why it must be done using ajax/javascript

                    Originally posted by Dormilich
                    Java and Javascript have only the first four letters in common ;). besides that, Javascript is an essential part of AJAX (thus the name).

                    if the javascript is too much for you consider using a Javascript library (like ExtJS, JQuery, Prototype, …) they should provide functions to make AJAX actions easier.
                    Asynchronous Javascript And Xml = AJAX

                    Comment

                    • angelicdevil
                      New Member
                      • Apr 2009
                      • 116

                      #11
                      jesus i m stuck then...does anyone of u know javascript ?

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #12
                        Head to the Javascript forum, and ask there for help.

                        Comment

                        Working...