Help using two arrays in one foreach

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nlkush
    New Member
    • Jan 2013
    • 1

    Help using two arrays in one foreach

    I have two arrays:

    1)
    Code:
    $labels = array   ("firstname" => "First Name: ",      //used for labeling a form
    				"lastname" => "Last Name: ",
    				"username" => "User Name: ",
    				"password" => "Password: ",
    				"email" => "Email: "
    				);
    2)
    Code:
     $_POST = array   ("firstname" => "john ",      //normal output from a form
    				"lastname" => "doe ",
    				"username" => "johndoe ",
    				"password" => "john123 ",
    				"email" => "john@email.com "
    				);
    I then do some validation. If there is trouble, the form outputs again with this :

    Code:
    echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";
    
    	foreach($labels as $field => $label)
        {
        	echo "<label for='$field'>$label</label>
                    <input type='text' name='$field' id='$field' value='$value'/>\n";
        }
        echo "<div id='submit'><input type='submit' value='Submit' />\n";
        echo "</form>\n</body>\n</html>";
     echo "There were errors with the data you provided:\n";
    The problem with this is every time the form is re-outputted the error message pops up, but all the information disappears. The reason this is happening is because as the form is being redisplayed, the $value variable is not being reset with the $_POST data. Is there anyway i can use a foreach statement to both output the form labels, and set the $value variable with the data stored in $_POST?
    Last edited by Meetee; Jan 10 '13, 04:59 AM. Reason: Use code tags <code/> around code
Working...