dfowler Posted March 6, 2008 Share Posted March 6, 2008 Hey guys, I created a dynamic form with the following code: $sql="SELECT * FROM ncsd_registration_fields WHERE sem_id='".$_GET['sem_id']."'"; $result=mysql_query($sql) or die('Error - '.mysql_error()); while($row=mysql_fetch_array($result)) { for($i=1; $i<=100; $i++){ if($row['fieldlabel_'.$i]){ if($row['fieldtype_'.$i] == 'text'){ if($select_name){ $select_name = ''; echo "</select>"; echo "</td>"; echo "</tr>"; } echo "<tr>\n"; echo "<td class='normalgray10'><strong>".$row['fieldlabel_'.$i].":</strong> <input type='text' name='".$row['fieldname_'.$i]."' value='".$row['fieldvalue_'.$i]."' /></td>\n"; echo "</tr>\n"; } else if($row['fieldtype_'.$i] == 'textarea'){ if($select_name){ $select_name = ''; echo "</select>"; echo "</td>"; echo "</tr>"; } echo "<tr>\n"; echo "<td class='normalgray10'><strong>".$row['fieldlabel_'.$i].":</strong> <textarea name='".$row['fieldname_'.$i]."'>".$row['fieldvalue_'.$i]."</textarea></td>\n"; echo "</tr>\n"; } else if($row['fieldtype_'.$i] == 'radio'){ if($select_name){ $select_name = ''; echo "</select>"; echo "</td>"; echo "</tr>"; } echo "<tr>\n"; echo "<td class='normalgray10'><strong>".$row['fieldlabel_'.$i].":</strong> <input type='radio' name='".$row['fieldname_'.$i]."' value='".$row['fieldvalue_'.$i]."' /></td>\n"; echo "</tr>\n"; } else if($row['fieldtype_'.$i] == 'select'){ if(!$select_name){ $select_name = $row['fieldname_'.$i]; echo "<tr>\n"; echo "<td class='normalgray10'><strong>Select:</strong><select name='".$select_name."'>\n"; echo "<option value='".$row['fieldvalue_'.$i]."'>".$row['fieldlabel_'.$i]."</option>\n"; } else if($select_name == $row['fieldname_'.$i]){ echo "<option value='".$row['fieldvalue_'.$i]."'>".$row['fieldlabel_'.$i]."</option>\n"; } else if($select_name != $row['fieldname_'.$i]){ $select_name = $row['fieldname_'.$i]; echo "<tr>\n"; echo "<td class='normalgray10'><strong>Select:</strong><select name='".$select_name."'>\n"; echo "<option value='".$row['fieldvalue_'.$i]."'>".$row['fieldlabel_'.$i]."</option>\n"; } } else if($row['fieldtype_'.$i] == 'checkbox'){ if($select_name){ $select_name = ''; echo "</select>\n"; echo "</td>\n"; echo "</tr>\n"; } echo "<tr>\n"; echo "<td class='normalgray10'><strong>".$row['fieldlabel_'.$i].":</strong> <input type='checkbox' name='".$row['fieldname_'.$i]."' value='".$row['fieldvalue_'.$i]."' /></td>\n"; echo "</tr>\n"; } } } } Here is my problem. On the next page where I process the form I am not going to know what the names of the fields are. So I can't simply say $_POST['name']. What do I do? Quote Link to comment Share on other sites More sharing options...
bpops Posted March 6, 2008 Share Posted March 6, 2008 You should grab the fieldnames from your database again. Quote Link to comment Share on other sites More sharing options...
dfowler Posted March 6, 2008 Author Share Posted March 6, 2008 Thanks, I don't know why I didn't think of that. Here is what I ended up with: $sql2="SELECT * FROM ncsd_registration_fields WHERE sem_id='".$_POST['sem_id']."'"; $result2=mysql_query($sql2) or die('Error - '.mysql_error()); while($row=mysql_fetch_array($result2)) { for($i=1; $i<=100; $i++){ if($row['fieldlabel_'.$i]){ if (!$var) { $var = $row['fieldname_'.$i]; } else if ($var == $row['fieldname_'.$i]) { $var = ''; } else { $var = $row['fieldname_'.$i]; } $$row['fieldname_'.$i] = $_POST[$var]; } } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.