www.webdeveloper.com
Results 1 to 3 of 3

Thread: dynamically number dropdown ID's linked to check boxes

  1. #1
    Join Date
    Apr 2008
    Posts
    44

    dynamically number dropdown ID's linked to check boxes

    Hi
    I need to be able to dynamically number drop down lists which are created from a mysql database, to make matters more confusing I also need to link the list to a checkbox, so that each checkbox has a dropdown list.
    The checkboxes are dynamically assigned an ID, but I am lost how to connect the two.

    Can anyone help?

    Dave

  2. #2
    Join Date
    Apr 2007
    Posts
    1,664
    without seeing more of your code I'm not sure exactly what you're trying to achieve but you can generate a unique id for each set using something like a counter in a loop, or progression from a set variable eg $id=0 then $id++ when you need to increase it to keep it unique.

    You can then use it to relate to any form element by appending it to the name.

    eg
    PHP Code:
    <?
    $id
    =0;
    ?>
    <select name="list_<?=$id?>"> 
        <option...>etc
    </select>
    <input type=checkbox name="checkbox_<?=$id?>" /> 

    <?
    $id
    ++;//$id now=1
    ?>
    <select name="list_<?=$id?>"> 
        <option...>etc
    </select>
    <input type=checkbox name="checkbox_<?=$id?>" />
    Of course it would make sense to keep the drawing of the different elements dynamic (in a loop) so you're not limiting yourself.

    So now you have a unique name for each element on the page that follow a set of rules and have a unique id associated with each one.

    After the form is posted explode() on the underscore to retrieve each part of the identifier.

  3. #3
    Join Date
    Apr 2008
    Posts
    44
    Many thanks
    Here is my code
    test page
    Code:
    <?
    include 'testdoggy.php';
    error_reporting(E_ALL ^ E_NOTICE); 
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <html>
    <script>
    function showMe (it, box) {
      var vis = (box.checked) ? "block" : "none";
      document.getElementById(it).style.display = vis;
    }
    </script>
    </head>
    <body>
    <form action="test.php" method="post" enctype="multipart/form-data" class="style2"><table width="390" border="0">
      <tr>
        <td>dog name</td>
        <td><input type="text" size="20" maxlength="20" name="dogs" <?php if (isset($_POST['dogs'])) { ?> value="<?php echo $_POST['dogs']; ?>"
    	<?php } ?>/></td>
      </tr>
      <tr>
        <td><? echo breeds() ?> </td>
        <td><div id="div1" style="display:none">
    <? echo classes() ?>
    </div></td>
      </tr>
      <tr>
        <td><input type="submit" name="submit" value="Submit" /></td>
        <td></td>
      </tr>
      <tr>
        <td width="182"></td>
        <td width="198">    </td>
      </tr>
    </table>
    </form>
    </body>
    </html>
    and my function page is
    Code:
    <?
    include 'includes/db.inc.php';
    
    function breeds(){
    	$returnstr='';
        $query = "select breedsID, breedy from doggybreed order by breedy";
        $result = @mysql_query($query);
    		  $ii=0;
    	while($breeds2=mysql_fetch_assoc($result)){
    		$returnstr= $returnstr.'<label><input type="checkbox" name="breedsID_'.$ii.'" value="'.$breeds2['breedsID'].'" onclick="showMe(\'div1\', this)" >'.$breeds2['breedy'].'</label><br />';
    		$ii++;
    	}
    	$returnstr= $returnstr.'<input type="hidden" name="breedcount" value="'.$ii.'"/>';
    
    		return $returnstr;
    }
    
    function classes(){
    	$returnstr='<select name="class1">';
        $query = 'select classID, class from doggyclass order by class';
        $result = @mysql_query($query);
    		  
    	while($areas=mysql_fetch_array($result)){
    		$returnstr= $returnstr.'<option value="'.$areas['classID'].'">'.$areas['class'].'</option>';
    	}
    
    		$returnstr= $returnstr.'</select>';
    		return $returnstr;
    }
    
    if (!isset($_POST['submit']))
    {
    	
    	// Store the information in the database
    	$query = "insert into doggys (dogs)values ('$_POST[ndogs]')";
    		mysql_query ($query) or die ('Could not add dog.');
    
    
    	$query="select LAST_INSERT_ID() as id";
    
    	$result=mysql_query ($query);
    
    
    	if (mysql_num_rows($result) > 0)
    
    	{
    
    		$dogID = mysql_fetch_array($result);
    
    		$dogID=$dogID['id'];
    
    
    	}
    	$count=count($breed);
    
    	//echo 'count='.$_POST['breedcount'].'--';
    
    	for($i=0;$i<$_POST['breedcount'];$i++){
    		if(isset($_POST['breedsID_'.$i])){
    			$query= "insert into doggybreedy(dogID,breedsID, classID) values($judgeID, '$_POST[class1]',".$_POST['breedid_'.$i].'  );';
    			mysql_query ($query) or die ('Could not add judge breed.');
    		}
    
    	}
    	}
    	
    ?>
    The idea is that each dynamically generated checkbox has a dropdown field so that when a user enters a name, selects a checkbox and then selects from the dropdown list, the options are sent to the database

    Dave

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 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