Jump to content

[SOLVED] Insert values into DB from dynamically created radio buttons


knox203

Recommended Posts

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

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. 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.