0

i'm generating a dynamic form using php. but my problem is how to store each field values into a multidimension array of unknown size.

to be clear:
1. i enter the number of entries i wish to submit
2. php will then generate a single form, but with specified number of set of fields i entered before.

much clearer:
1. how many records you wish to add? i enter 5.
2. php will generate 5 sets of these fields:
-----name:
-----address:
-----position:

what i plan is this:

<?php if(!isset($_POST['noRec']) || (isset($_POST['noRec']) && empty($_POST['noRec'])) || isset($_POST['noRec']) && $_POST['noRec']<0)
	{ ?>
    <form method="post" action="<?php $_SERVER['PHP_SELF'];?>">
        <span>How many employee do you want to add? </span>
        <input type="text" size="5" maxlength="2" name="noRec" /><br />
        <input type="submit" value="Add Record" />
    </form>
<?php }
	elseif(isset($_POST['noRec'])) { ?>
    	<form method="post" action=""> <?php
	for($i=0;$i<$_POST['noRec'];$i++) { echo "\n"; ?>
        	<table>
            	<tr>
                	<td class="left">Employee No:</td>
                	<td><input type="text" name="<?php echo "em[$i][emNo]"; ?>" size="7" maxlength="2" /></td>
                </tr>
            	<tr>
                	<td class="left">First Name:</td>
                	<td><input type="text" name="<?php echo "em[$i][fName]"; ?>" /></td>
                </tr>
            	<tr>
                	<td class="left">Last Name:</td>
                	<td><input type="text" name="<?php echo "em[$i][lName]"; ?>" /></td>
                </tr>
            	<tr>
                	<td class="left">Position:</td>
                	<td><input type="text" name="<?php echo "em[$i][pos]"; ?>" /></td>
                </tr>
            	<tr>
                	<td class="left">Department:</td>
                	<td><input type="text" name="<?php echo "em[$i][dept]"; ?>" /></td>
                </tr>
            </table>
        <hr noshade="noshade" />
<?php 	} echo "\n"; ?>
        <input type="submit" />
        </form>
         <?php
    }?>

i intend to group the related records together. say:

$record = array( array('emNo' => '', 
                       'fName' => '', 
                       'lName' => '', 
                       'pos' => '', 
                       'dept' => '' ) );

but i don't know if this is possible. because i want to use emNo, fname, lname, pos, dept as keys.

i want to reference a field value by this:

echo "$record[0]['fName']";

hmm maybe that's all i want to clarify. now what i want to find out are:
1. how to declare/create an array of unknown size? since form fields will be generated dynamically during php runtime
2. how to make a multidimensional array that has an index key and an associative key? eg: $record[0]

please help. THANKS A LOT.
- michael gerona

3
Contributors
12
Replies
14
Views
8 Years
Discussion Span
Last Post by scias23
0

Your example should work the way you want it. There is no dimensioning of arrays in PHP, space is allocated as necessary.

0

but how will i output those form fields? i dont know how. i tried to use print_r($_POST), it works. but i want to present the values like:

Employee NUmber: 1
FIrst name: Michael
Last name: gerona
etc: etc

??

Your example should work the way you want it. There is no dimensioning of arrays in PHP, space is allocated as necessary.

0

but how will i output those form fields? i dont know how. i tried to use print_r($_POST), it works. but i want to present the values like:

Employee NUmber: 1
FIrst name: Michael
Last name: gerona
etc: etc

??

Try adding this at the end of your code:

<?php
if (!empty($_POST)) {
for ($i=0;isset($_POST['em'][$i]);$i++) {
    echo '<hr>Employee Num: '.$_POST['em'][$i]['emNo'];
    echo '<br>First Name: '.$_POST['em'][$i]['fName'];
    echo '<br>Last Name: '.$_POST['em'][$i]['lName'];
    echo '<br>position: '.$_POST['em'][$i]['pos'];
    echo '<br>dept: '.$_POST['em'][$i]['dept'];
    }
    }
?>
0

thanks. it works. but what if i do this using foreach?

i tried this one but it won't work.

foreach($em as $record) {
	foreach($record as $key => $value) {
		echo "$key is $value<br/>";
	}
}

any suggestions?

Try adding this at the end of your code:

<?php
if (!empty($_POST)) {
for ($i=0;isset($_POST['em'][$i]);$i++) {
    echo '<hr>Employee Num: '.$_POST['em'][$i]['emNo'];
    echo '<br>First Name: '.$_POST['em'][$i]['fName'];
    echo '<br>Last Name: '.$_POST['em'][$i]['lName'];
    echo '<br>position: '.$_POST['em'][$i]['pos'];
    echo '<br>dept: '.$_POST['em'][$i]['dept'];
    }
    }
?>
0

oh nevermind. problem solved. i just changed $em to $_POST

thanks guys!

0

oh, another problem. i want to validate the form values. but i can't get the validation message to show up.

<?php 

	if(!isset($_POST['noRec']) || (isset($_POST['noRec']) && empty($_POST['noRec'])) || isset($_POST['noRec']) && $_POST['noRec']<0)
	{ 	?>
    <form method="post" action="<?php $_SERVER['PHP_SELF'];?>">
        <span>How many employee do you want to add? </span>
        <input type="text" size="5" maxlength="2" name="noRec" /><br />
        <input type="submit" value="Add Record" />
    </form>
<?php }
	elseif(isset($_POST['noRec'])) { ?>
    	<form method="post" action="<?php if(empty($_POST['em'])) {$_SERVER['PHP_SELF']; } else { echo "process.php"; } ?>"> <?php
	for($i=0;$i<$_POST['noRec'];$i++) { echo "\n"; ?>
        	<table>
            	<tr>
                	<td class="left">Employee No:</td>
                	<td><input type="text" name="<?php echo "em[$i][emNo]"; ?>" value="<?php echo $i+1; ?>" size="3" maxlength="2" />
					</td>
                </tr>
            	<tr>
                	<td class="left">First Name:</td>
                	<td><input type="text" name="<?php echo "em[$i][fName]"; ?>" <?php if( isset($_POST['em'][$i]['fName'])) { echo 'value="'.htmlentities($_POST['em'][$i]['fName']).'"'; } ?> />
					<?php if(isset($_POST['em'][$i]['fName']) && empty($_POST['em'][$i]['fName']))
							{
								echo "Fill this up.";
							}
					?>
					</td>
                </tr>
            	<tr>
                	<td class="left">Last Name:</td>
                	<td><input type="text" name="<?php echo "em[$i][lName]"; ?>"  <?php if( isset($_POST['em'][$i]['lName'])) { echo 'value="'.htmlentities($_POST['em'][$i]['lName']).'"'; } ?> />
					<?php if(isset($_POST['em'][$i]['lName']) && empty($_POST['em'][$i]['lName']))
							{
								echo "Fill this up.";
							}
					?>
					</td>
                </tr>
            	<tr>
                	<td class="left">Position:</td>
                	<td>
                    	<select name="<?php echo "em[$i][pos]"; ?>" />
                        	<option> </option>
                            <option value="Under Secretary" <?php if( isset($_POST['em'][$i]['pos']) && $_POST['em'][$i]['pos']=="Under Secretary") { echo 'selected'; } ?> >Under Secretary</option>
                            <option value="Assistant Secretary" <?php if( isset($_POST['em'][$i]['pos']) && $_POST['em'][$i]['pos']=="Assistant Secretary") { echo 'selected'; } ?> >Assistant Secretary</option>
                        </select>
					<?php if(isset($_POST['em'][$i]['pos']) && empty($_POST['em'][$i]['pos']))
							{
								echo "Fill this up.";
							}
					?>
                    </td>
                </tr>
            	<tr>
                	<td class="left">Department:</td>
                	<td>
                    	<select name="<?php echo "em[$i][dept]"; ?>" />
                        	<option> </option>
                            <option value="IT EDP" <?php if( isset($_POST['em'][$i]['pos']) && $_POST['em'][$i]['dept']=="IT EDP") { echo 'selected'; } ?> >IT EDP</option>
                            <option value="MIS" <?php if( isset($_POST['em'][$i]['pos']) && $_POST['em'][$i]['dept']=="MIS") { echo 'selected'; } ?> >MIS</option>
                        </select>
					<?php if(isset($_POST['em'][$i]['dept']) && empty($_POST['em'][$i]['dept']))
							{
								echo "Fill this up.";
							}
					?>
                    </td>
                </tr>
            </table>
        <hr noshade="noshade" />
<?php 	} echo "\n"; ?>
        <input type="submit" />
        </form>
         <?php
    }
?>

this happens:
1. i enter the number of records i want to input. lets say 1.
2. php generates 1 set of needed fields.
3. i leave SOME of the fields empty. others are filled up.
3. when i press the submit button, the script returns the "How many employee do you want to add?" form. instead of the validation messages.

what i want to happen is:
1. i enter the number of records i want to input. lets say 1.
2. php generates 1 set of needed fields.
3. i leave SOME of the fields empty. others are filled up.
4. i submit the form
5. php then displays the "FILL THIS UP" message next to each empty fields.
6. when all fields are filled up, php will then direct the form action to process.php

HELP

-michael gerona

0

I have added a javascript validation and the page is as follows:

<head><script>
function submitform() {
    var err;
    err=0;
    for (i=0; i<window.numrows;i++) {
        if (document.getElementById(i+"a").value == "") {
            document.getElementById('d'+i+'a').innerHTML="Fill this up.";
            err=1;
            } else {
            document.getElementById('d'+i+'a').innerHTML="";
            }
        if (document.getElementById(i+"b").value == "") {
            document.getElementById('d'+i+'b').innerHTML="Fill this up.";
            err=1;
            } else {
            document.getElementById('d'+i+'b').innerHTML="";
            }
        if (document.getElementById(i+"c").value == "") {
            document.getElementById('d'+i+'c').innerHTML="Fill this up.";
            err=1;
            } else {
            document.getElementById('d'+i+'c').innerHTML="";
            }
        if (document.getElementById(i+"d").value == "") {
            document.getElementById('d'+i+'d').innerHTML="Fill this up.";
            err=1;
            } else {
            document.getElementById('d'+i+'d').innerHTML="";
            }
        if (document.getElementById(i+"e").value == "") {
            document.getElementById('d'+i+'e').innerHTML="Fill this up.";
            err=1;
            } else {
            document.getElementById('d'+i+'e').innerHTML="";
            }
        }
    if (err==0) {
        document.mainvalidationform.submit();
        } else {
        alert("You need to fill in one or more fields");
        }
    }
</script></head><body>
<?php 

	if(!isset($_POST['noRec']) || (isset($_POST['noRec']) && empty($_POST['noRec'])) || isset($_POST['noRec']) && $_POST['noRec']<0)
	{ 	?>
    <form method="post" action="<?php $_SERVER['PHP_SELF'];?>">
        <span>How many employee do you want to add? </span>
        <input type="text" size="5" maxlength="2" name="noRec" /><br />
        <input type="submit" value="Add Record" />
    </form>
<?php }
	elseif(isset($_POST['noRec'])) { ?>
    	<form method="post" name="mainvalidationform" action="<?php if(empty($_POST['em'])) {$_SERVER['PHP_SELF']; } else { echo "process.php"; } ?>"> <?php
	for($i=0;$i<$_POST['noRec'];$i++) { echo "\n"; ?>
        	<table>
            	<tr>
                	<td class="left">Employee No:</td>
                	<td><input type="text" name="<?php echo "em[$i][emNo]"; ?>" value="<?php echo $i+1; ?>" size="3" maxlength="2" id="<?php echo $i;?>a" /></td>
					<td><div id="d<?php echo $i?>a"></div></td>
                </tr>
            	<tr>
                	<td class="left">First Name:</td>
                	<td><input type="text" name="<?php echo "em[$i][fName]"; ?>" <?php if( isset($_POST['em'][$i]['fName'])) { echo 'value="'.htmlentities($_POST['em'][$i]['fName']).'"'; } ?>  id="<?php echo $i;?>b" /></td>
                    <td><div id="d<?php echo $i?>b"></div></td>
                </tr>
            	<tr>
                	<td class="left">Last Name:</td>
                	<td><input type="text" name="<?php echo "em[$i][lName]"; ?>"  <?php if( isset($_POST['em'][$i]['lName'])) { echo 'value="'.htmlentities($_POST['em'][$i]['lName']).'"'; } ?>  id="<?php echo $i;?>c" /></td>
                    <td><div id="d<?php echo $i?>c"></div></td>
                </tr>
            	<tr>
                	<td class="left">Position:</td>
                	<td>
                    	<select name="<?php echo "em[$i][pos]"; ?>"  id="<?php echo $i?>d" />
                        	<option> </option>
                            <option value="Under Secretary" <?php if( isset($_POST['em'][$i]['pos']) && $_POST['em'][$i]['pos']=="Under Secretary") { echo 'selected'; } ?> >Under Secretary</option>
                            <option value="Assistant Secretary" <?php if( isset($_POST['em'][$i]['pos']) && $_POST['em'][$i]['pos']=="Assistant Secretary") { echo 'selected'; } ?> >Assistant Secretary</option>
                        </select></td>
                    <td><div id="d<?php echo $i;?>d"></div>
                    </td>
                </tr>
            	<tr>
                	<td class="left">Department:</td>
                	<td>
                    	<select name="<?php echo "em[$i][dept]"; ?>"  id="<?php echo $i?>e" />
                        	<option> </option>
                            <option value="IT EDP" <?php if( isset($_POST['em'][$i]['pos']) && $_POST['em'][$i]['dept']=="IT EDP") { echo 'selected'; } ?> >IT EDP</option>
                            <option value="MIS" <?php if( isset($_POST['em'][$i]['pos']) && $_POST['em'][$i]['dept']=="MIS") { echo 'selected'; } ?> >MIS</option>
                        </select>
					</td>
                    <td><div id="d<?php echo $i;?>e"></div></td>
                </tr>
            </table>
        <hr noshade="noshade" />
<?php 	} echo "\n<script>var numrows=$i;</script>"; ?>
        <input type="button" value="Submit" onclick="javascript:submitform();"/>
        </form>
         <?php
    }
?></body>
0

hey thanks! this works. :)

but would you mind? how'd i do this validation using php?

0

hey thanks! this works. :)

but would you mind? how'd i do this validation using php?

Well you would need the page to post to itself and php would check if everything is filled in. Also you would need a hidden field with the number of times that php for loop loops for. And if php finds that all the fields are filled in you would then use header redirect to process.php but I would say that javascript is the better solution as there is way less pressure on the server.

0

i thought i'd be enlightened by this code i made.

if(isset($_POST['em'])) {
   foreach($_POST['em'] as $rec) {
      foreach($rec as $key => $value) {
         if($value==''){
            $check+=1;
         }
      }
   }
}

it checks each form object for empty fields, if it detects one, it adds 1 to the $check variable.

i can't figure out how to make this line work the way i want it to work.

<form method="post" action="<?php if(isset($check) && $check>0) {$_SERVER['PHP_SELF']; } else { echo "process.php"; } ?>">

when the script loads, the action attribute is already set to "process.php".

what i want to happen is:
1. if all fields are filled up, the form's action attribute is set to "process.php"
2. if some of the fields are blank, the form's action attribute is set to $_SERVER

any thoughts?

0

it appears that no one is replying to this thread anymore. but anyways, i hope somebody answers my question above.

0

my problem is partially solved. thanks guys. i'm learning.

but please somebody answer my question haha.

shrek

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.