ttmt Posted January 26, 2009 Share Posted January 26, 2009 Is it possible to generate dynamic names for input checkbox forms ? I'm using this code to generate a form of text fields and checkboxs <?php require_once("includes/connection.php"); require_once("includes/functions.php"); include("includes/header.php"); ?> <?php $query = "SELECT key_id, words FROM keywords"; $result = mysql_query($query); confirm_query($result); $options = ""; while($row = mysql_fetch_array($result)){ $options .= "{$row[1]}:<input type=\"checkbox\" value=".$row[0]." name=\"section[]\" />\n"; } $number_of_uploads = 3; ?> <body> <div id="con"> <form name="upload_form" action="upload.php" method="post" enctype="multipart/form-data"> <?php for($counter = 1;$counter<=$number_of_uploads;$counter++){ ?> <p> <b>Image:</b> <textarea name="title[]" cols="30" rows="1"></textarea> </p> <p> <b>Sections:</b> <?php echo $options ;?> </p> <?php } ?> <br/> <p> <input type="submit" name="submit" value="Add Photos" /> </p> </form> </div> </body> </html> The while loop generates the checkbox's and the values from all the checkbox's are stored in section[] which can them be used from $_POST['section']. while($row = mysql_fetch_array($result)){ $options .= "{$row[1]}:<input type=\"checkbox\" value=".$row[0]." name=\"section[]\" />\n"; } What I want to be able to do is create a seperate array for each loop of the while loop so the name might be something like name=\"section[].$row[0]\" Link to comment https://forums.phpfreaks.com/topic/142494-dynamic-name-checkbox/ Share on other sites More sharing options...
flyhoney Posted January 26, 2009 Share Posted January 26, 2009 while($row = mysql_fetch_array($result)){ $options .= $row[1] . ':<input type="checkbox" value="'.$row[0].'" name="section['.$row[0].']" />'; } Link to comment https://forums.phpfreaks.com/topic/142494-dynamic-name-checkbox/#findComment-746669 Share on other sites More sharing options...
ttmt Posted January 26, 2009 Author Share Posted January 26, 2009 Thanks for the reply flyhoney I actual tried doing it this way but couldn't work out how to access the array, I was trying this <?php echo "<pre>"; print_r($_POST['section'][0]); echo "</pre>"; ?> How should I access the array to use the info. Link to comment https://forums.phpfreaks.com/topic/142494-dynamic-name-checkbox/#findComment-746697 Share on other sites More sharing options...
flyhoney Posted January 27, 2009 Share Posted January 27, 2009 What does it look like when you do: <?php echo "<pre>"; print_r($_POST['section']); echo "</pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/142494-dynamic-name-checkbox/#findComment-747815 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.