Jump to content

Adding additional fields


clang

Recommended Posts

Right now I have a form that takes in information. This form is set up to take 10 sets of the same information. If you fill in none, or some of the sets, it continues on. If you fill in all 10, it also continues on, but there is no option to enter in more than 10.

What I would like to have happen is the default sets you enter in be 1. Then if you need more you press an "Additional" button, or something, and it updates to give you another set that you can fill in.

The form will then be processed by a php script, which will need to know how many sets were created.

 

I'll attach the code for what I have already.

 

This is the processing script. Notice the use of $numberOfAdditionalRows to determine how many rows exist. This is just a static value right now.

<?php
$numberOfAdditionalRows=10;
if($_SESSION['documentInfoSet'])
{
if(isset($_POST['Enter']))
{
	$documentInfo=$_SESSION['documentInfo'];
	$county=$documentInfo[1][0];
	$c=2;
	for($b=0;$b<$numberOfAdditionalRows;$b++)
	{
		$wasSet=FALSE;

		$_POST['documentNumber'.$b]=removeBlankSpaces($_POST['documentNumber'.$b]);

		if($_POST['documentNumber'.$b]!='')
		{
			if(validNumber($_POST['documentNumber'.$b]))
			{
				$documentInfo[$c][1]=$_POST['documentNumber'.$b];
				$wasSet=True;
			}
			else
			{
				$error='Invalid Entry. Please make sure to only use numbers for Document Number, Volume, and Page.';
			}
		}

		if($wasSet)
		{
			$documentInfo[$c][0]=$county;
			$c++;
		}	
	}
	if(!isset($error))
	{
		if(isset($_SESSION['documentInfoSet']))
		{
			unset($_SESSION['documentInfoSet']);
		}
		$_SESSION['documentInfoSet']=TRUE;
		$_SESSION['documentInfo']=$documentInfo;
		header("Location: ./Upload.php", true, 302 );
	}
}
}
?>

 

 

This is the front end which displays the html form. $numberOfAdditionalRows is used again here.

<form action="./Additional.php" method="post" name="form">
<table align="center" border="1">
<?php
for($a=0;$a<$numberOfAdditionalRows;$a++)
{
?>
<tr><th colspan="4"><?php echo ($a+1); echo ")";?></th></tr>
<tr>
<th> Document Number: </th>
<td> <input type="text" name="documentNumber<?php echo $a;?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="10" align="center">
<input type="submit" name="Enter" value="Enter" >
</td>
</tr>
</table>
</form>

 

I know php fairly well, but I'm not at all experienced with AJAX or JavaScript. All help is appreciated.

Link to comment
Share on other sites

  • 2 weeks later...

This is the code I used to kinda 'fix' the problem...I don't think it's exactly what you want...but change a few things here and there and it might give you what you want..

 

function createInput(name,div) 
{
		var sel = document.getElementById(name);
		var number = sel.options[sel.selectedIndex].value;

		var element = document.getElementById("images");
		element.innerHTML = "";

		for( var i=1; i <= number; i++)
			{	
				var newDiv = document.createElement('div');
				if( i == 1)
					{
						newDiv.setAttribute("class","fm-req");
					}
			  	else 
					{
						newDiv.setAttribute("class","fm-opt");
					}

				var newLabel = document.createElement('label');
				newLabel.setAttribute("for","image"+i);
				newLabel.innerHTML = "Image " + (i)  + ":";
			   
				newDiv.appendChild(newLabel);
			   
				var newInput = document.createElement('input');
				newInput.setAttribute("id","image"+1);
				newInput.setAttribute("name","images[]");
				newInput.setAttribute("type","file");
			   
				newDiv.appendChild(newInput);

				document.getElementById(div).appendChild(newDiv);
			}
} 

 

for processing all of the inputs look at the PHP manual:

 

it gives you something like this for multiple information/files

 

		foreach ($_FILES["files"]["error"] as $key => $error) 
			{
				if ($error == UPLOAD_ERR_OK) 
					{
						$temp_name = $_FILES["files"]["tmp_name"][$key];
						$name = $_FILES["thumbnail"]["name"][$key];
						$path = $thumb_name;	 // where you want to upload it to					
						move_uploaded_file($thumb_temp_name, $thumb_path);
					}
			}	

 

maybe you can give the fields name of  "fields[]" and process them like the code above using the foreach loop??

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.