/    Sign up×
Community /Pin to ProfileBookmark

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

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@SyCoApr 08.2008 — 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
[code=php]<?
$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?>" />
[/code]


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.
Copy linkTweet thisAlerts:
@dag78authorApr 08.2008 — 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>[/CODE]


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.');
}

}
}

?>[/CODE]


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
×

Success!

Help @dag78 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...