Checking for submitted form values within array

I have the following form:

<form action="index.php" method="post">

<div class="block l">
<label>Enter E-mail Address: <input type="text" maxlenth="60" name="raw_new_member['email']" /></label>
<label>Create a Password: <input type="password" maxlenth="60" name="raw_new_member['pass']" /></label>
<label>Create a Username: <input type="text" maxlenth="60" name="raw_new_member['user']" /></label>

<div class="block r">	
<label>Retype E-mail Address: <input type="text" maxlenth="60" name="raw_new_member['email_copy']" /></label>			
<label>Re-enter a Password: <input type="password" maxlenth="60" name="raw_new_member['pass_copy']" /></label>					
<label>Re-enter Username: <input type="text" maxlenth="60" name="raw_new_member['user_copy']" /></label>

<input class="submit" type="submit" value="Submit" name="submit" />

This form basically gets submitted to the same page it’s on and what I need to do is create a way to do a check-and-compare for the submitted values–hence, the pass and pass_copy values, user and user_copy values, and so on…

SO, with that being said, at the top of the PHP stuff, I have the following:

foreach($_POST['raw_new_member'] as $k=>$v){
		if(isset($_POST[$k]) && $_POST[$k] !== ''){
			echo 'POST is set and has something!<br />';
			$clean_new_member = $_POST['raw_new_member'];
			echo 'Go back!<br />';

This PHP’s purpose is to make me come to SitePoint and admit defeat, but the intended purpose is to take those array values from the form above and check to ensure 2 main things: A.) They are set, and B.) They are not empty. Should be pretty easy, right? Well, guess what?

I’m dropping the ball here and I think it’s lack understanding of how to properly target the right part of the array when cycling through everything…

Anyway, it’s not working and I would appreciate any insight. :slight_smile:

Your array structure just won’t work if you tried to add another set of those inputs. The values would be overwritten.

<input name="users[0][username]">
<input name="users[0][username_copy]">
<input name="users[0][pass]">
<input name="users[0][pass_copy]">


<input name="users[1][username]">
<input name="users[1][username_copy]">
<input name="users[1][pass]">
<input name="users[1][pass_copy]">

Note that it looks similar to php code. Maybe this observation can help you.

$users = array();
$users[0]['username'] = 'foo';
$users[0]['username_copy'] = 'foo';
$users[0]['pass'] = 'foo';
$users[0]['pass_copy'] = 'foo';

$users[1]['username'] = 'foo';
$users[1]['username_copy'] = 'foo';
$users[1]['pass'] = 'foo';
$users[1]['pass_copy'] = 'foo';


foreach ($_POST['users'] as $user) {
    echo $user['username'];
    echo $user['username_copy'];

You don’t want the quote characters in the names
should be

Quotes just aren’t needed in this context, and php will not strip them out for you. They will end up as part of the array key.

You need a different array structure too. But before I go on, do you really need to use the array naming syntax feature? Is there more to this that you haven’t shown us? If maybe that form might be used to register multiple new users at the same time, then I’d say it’s a good candidate for using this feature. But otherwise, I wouldn’t.

I guess I don’t know my array stuff like I should, but I’m getting there… :wink: I’ll remember that about the quotes.

As for the name stuff, I’m not really sure if I need this or not. What would you suggest? The form above that we’re talking about here is for a part of this website I’m working on that installs the website. So in other words, it will only create 1 person, but in the long-run, I would like to extend it to allow for multiple registrations–which would be cause for naming the keys.

What do you mean by array structure? If you’re speaking of the name of the array, the reason I chose to name it “raw_new_member” is because it’s coming from an unsanitized input and it’s from the “new member” form.

What if these inputs are only used during an install?

What your probably after is something like the below.

// form action
$strAction = $_SERVER['PHP_SELF'];

// base $_POST key for users
$strBaseKey = 'raw_new_memeber';

// post fixes for numbers
$arrPostFix = array('st','nd','rd','th','th','th','th','th','th');

// numbers of users
$intUsers = 3;

// form configuration
$arrConfigs = array(
		'label'=>'Enter E-mail Address'
		'label'=>'Create a Password'
		'label'=>'Create a Username'
		'label'=>'Retype E-mail Address'
		'label'=>'Re-enter a Password'
		'label'=>'Re-enter Username'

// form values
$arrValues = array();

// form errors
$arrErrors = array();
$boolError = false;

// determine form values for users
for($i=0;$i<$intUsers;$i++) {

	$arrValues[$i] = array();
	$arrErrors[$i] = array();

	foreach(array_keys($arrConfigs) as $strField) {
		$arrValues[$i][$strField] = isset($_POST[$strBaseKey],$_POST[$strBaseKey][$i],$_POST[$strBaseKey][$i][$strField])?$_POST[$strBaseKey][$i][$strField]:'';
		if(isset($_POST[$strBaseKey]) && strcmp($arrConfigs[$strField]['required'],'Y') == 0 && strlen($arrValues[$i][$strField]) == 0) {
			$boolError = true;
			$arrErrors[$i][$strField] = "Field {$arrConfigs[$strField]['label']} is required";


// save data when no errors exist
if($boolError === false && isset($_POST[$strBaseKey])) {
	$arrSave = array();
	foreach($arrValues as $arrUser) {
		$arrCols = array_diff(array_keys($arrUser),array('user_copy','pass_copy','email_copy'));
		// @TODO: clean data before imploding
		$arrSave[] = "('".implode("','",array_diff_key($arrUser,array('user_copy'=>'','pass_copy'=>'','email_copy'=>'')))."')";
	$strSQL = sprintf(
		'INSERT INTO &#37;s (%s) VALUES %s'
	echo "<p>$strSQL</p>";

* --------------------------------- Template -----------------------------------
<html lang="en">
	<meta http-equiv="content-type" content="text/html; charset=utf-8">
	<style type="text/css">
		form fieldset input.invalid {
			border: 1px solid red;
// print errors
foreach($arrErrors as $intUser=>$arrError) {
	if(empty($arrError)) continue;
	echo '<p>Please fill out all required fields for user ',($intUser+1),' before proceeding.</p>';

// build form
printf('<form action="%s" method="%s">',$strAction,'POST');
foreach($arrValues as $intUser=>$arrUser) {
	printf('<fieldset id="user-%s-registration">',($intUser+1));
	printf('<legend>Register %s%s User</legend>',($intUser+1),$arrPostFix[$intUser]);
	foreach($arrConfigs as $strField=>$arrConfig) {
		if(in_array($strField,array('email','email_copy'))) {
			printf('<div class="block %s">',strcmp($strField,'email') == 0?'l':'r');
			'<label for="user-%s-%u">%s%s: <input type="%s" name="%s[%u][%s]" maxlength="%u" value="%s" id="user-%1$s-%2$u"%s></label>'
			 ,strcmp($arrConfig['required'],'Y') == 0?'<span class="required">*</span>':''
		if(in_array($strField,array('user','user_copy'))) {
			echo '</div>';
	echo '</fieldset>';

echo '<input class="submit" type="submit" value="Submit" name="submit" />';
echo '</form>';