Jump to content

PHP in a form problem


newtoall

Recommended Posts

I hope someone who has a bit more PHP knowledgeable than me can explain why the following is happening and suggest a wat to fix it…..:

 

Basically I have a form that contains a table and is used to update information in a mysql database. The form displays multiple rows of information and in the following example seems to work OK (I have chopped the table down a bit):

<form name='timesheetForm' method='post' action=''>
 <table bgcolor='#CCD6E4'>
 <tr>
   <tr>
     <td><font size="2" face="Arial, Helvetica, sans-serif"> Project code</td>
     <td><font size="2" face="Arial, Helvetica, sans-serif">Monday</td>
     <td><font size="2" face="Arial, Helvetica, sans-serif">Totals</td>
   </tr>
  <td><input type='text' name='ProjectCode[]' value="<?php echo $TimesheetP[$i][0];?>" size=10 maxlength=10></td>
   <td><input type='text' name='MonTime[]' value="<?php echo $TimesheetP[$i][5];?>" size=10 maxlength=10></td>
    <td><input type='text' name='TotalTime[]' value="<?php echo $TimesheetP[$i][12];?>" size=10 maxlength=10></td>
    </tr>
  <?php
   }
   ?>   
  <tr>
   <td colspan=9 align='right'>
    <input type='submit' value='Submit Timesheet'></td></tr>
</table></form>

 

However when I add a select value into the table only one row is returned, and it looks like the select stops the outer loop from working, I was hoping someone could tell me why??:

 

<form name='timesheetForm' method='post' action=''>
 <table bgcolor='#CCD6E4'>
 <tr>
   <tr>
     <td><font size="2" face="Arial, Helvetica, sans-serif"> Project code</td>
     <td><font size="2" face="Arial, Helvetica, sans-serif">Monday</td>
     <td><font size="2" face="Arial, Helvetica, sans-serif">Totals</td>
   </tr>
  <?php
    for($i = 0; $i<$TimesheetPs; $i++) {
    ?>
  <tr>
  <td><select name = "ProjectCode[]"> <?php
     for($i = 0; $i<$project_lists; $i++)
     {
       echo '<option value = '.$project_list[$i][0];
       if ($TimesheetP[$i][0] == $project_list[$i][0]) echo ' selected';
       echo '>'.$project_list[$i][1]."</option>\n";
     }
     ?></select></td>    <td><input type='text' name='MonTime[]' value="<?php echo $TimesheetP[$i][5];?>" size=10 maxlength=10></td>
    <td><input type='text' name='TotalTime[]' value="<?php echo $TimesheetP[$i][12];?>" size=10 maxlength=10></td>
    </tr>
  <?php
   }
   ?>   
  <tr>
   <td colspan=9 align='right'>
    <input type='submit' value='Submit Timesheet'></td></tr>
</table></form>

 

Any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/52455-php-in-a-form-problem/
Share on other sites

Here are some quick guesses:

 

for($i = 0; $i<$project_lists; $i++)
{
    echo '<option value = '.$project_list[$i][0];
    if ($TimesheetP[$i][0] == $project_list[$i][0]) 
    {
        echo ' selected';  //added some curly brackets around this, always good to do on an if statement
     }
     else {''} //what do you want it to do if this is not the first item?
    echo '>'.$project_list[$i][1]."</option>\n";
}

Link to comment
https://forums.phpfreaks.com/topic/52455-php-in-a-form-problem/#findComment-259093
Share on other sites

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.