Jump to content

Form Help


dfowler

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/94729-form-help/
Share on other sites

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];
	}
}
}

Link to comment
https://forums.phpfreaks.com/topic/94729-form-help/#findComment-485096
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.