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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.