knox203 Posted May 30, 2008 Share Posted May 30, 2008 Hello, I have some code that generates radio buttons with values from my MySQL DB inside a do-while loop. Here's the code: <?php do { $contractor = $ec_data['DriverCode']; ?> <tr> <td valign="top" class="border_l1"><span class="body_8"> <?php echo ereg_replace('99', 'C', $contractor); ?> </span></td> <td valign="top" class="border_l1"><span class="body_8"> <?php echo "Yes -<input name=\"$contractor\" id=\"$contractor\" type=\"radio\" value=\"Yes\" /> No -<input type=\"radio\" name=\"$contractor\" id=\"$contractor\" value=\"No\" />"; ?> </span></td> <td valign="top" class="border_l1"><span class="body_8"> <?php echo $ec_data['DispatchGroup']; ?> </span></td> </tr> <?php } while ($ec_data = mssql_fetch_assoc($ec_result)); mssql_close(); ?> Basically what this script will do is pull driver names from the database who has completed an order on the selected day, then generate a Yes/No radio button set for each, which I then want to submit to a database to tally how many days the driver worked more than 8 hours. Whats the best way to submit values from dynamically created form fields when they won't ever be the same? Am I explaining this correctly, or just making it sounds more confusing than it needs to be?? Thanks! - Adam Link to comment https://forums.phpfreaks.com/topic/108041-solved-insert-values-into-db-from-dynamically-created-radio-buttons/ Share on other sites More sharing options...
fanfavorite Posted May 30, 2008 Share Posted May 30, 2008 I am not following exactly what your database structure is, but if you have a few yes/no radio buttons for each driver, then I would do something like: <input name="<? echo $contractor[$count] ?>" type="radio" value="yes" /> <input name="<? echo $contractor[$count] ?>" type="radio" value="no" /> This will put each $contractor into an array. So say $contractor = Adam and you have 7 values (1 for each day of the week). foreach($_POST[$contractor] as $con) { $concount = 0; if ($con == "yes") { $concount++; } } mysql_query("INSERT INTO WorkingDays SET Contractor = '$contractor', DaysOver8Hrs = $concount"); This is just a shot in the dark, since I don't quite understand your database structures. Link to comment https://forums.phpfreaks.com/topic/108041-solved-insert-values-into-db-from-dynamically-created-radio-buttons/#findComment-553791 Share on other sites More sharing options...
knox203 Posted May 31, 2008 Author Share Posted May 31, 2008 Fanfavorite, thanks for your reply! My apologies for not explaining my situation clearly, but I was able to use your code... after a few slight modifications, I got it to work! Here's what I came up with (probably not the most efficient solution, but hey, it works!) On the form page: <?php do { $contractor = $ec_data['DriverCode']; ?> <tr> <td valign="top" class="border_l1"><span class="body_8"> <?php echo ereg_replace("99", "C", $contractor); ?></span></td> <td valign="top" class="border_l1"><span class="body_8"> <?php echo "Yes:<input name=\"$contractor\" type=\"radio\" value=\"Yes\" /> No:<input type=\"radio\" name=\"$contractor\" value=\"No\" />"; ?> </span></td> </tr> <?php } while ($ec_data = mssql_fetch_assoc($ec_result)); mssql_close(); ?> And on the submit page: <?php foreach($_POST as $var=>$val) { $ic_num = ereg_replace("99", "C", $var); if ( $ic_num == 'submit' && $val == 'Submit' ) { } else if ( $ic_num != 'submit' && $val == 'Yes' ) { $ic_query = mysql_query(" select max(days_worked) as daysworked from $databasename.ic_access where employee_number = '$ic_num'") or die (mysql_error()); $ic_result = mysql_fetch_assoc($ic_query); $add_day = $ic_result['daysworked'] + 1; mysql_query("UPDATE $databasename.ic_access SET days_worked = '$add_day' WHERE employee_number = '$ic_num'"); } } ?> Thanks again! - Adam Link to comment https://forums.phpfreaks.com/topic/108041-solved-insert-values-into-db-from-dynamically-created-radio-buttons/#findComment-553944 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.