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>

<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>
</div>

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

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'];
		}else{
			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';


print_r($_POST);

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

You don’t want the quote characters in the names
raw_new_member[‘email’]
should be
raw_new_member[email]

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.


<?php
// 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(
	'email'=>array(
		'label'=>'Enter E-mail Address'
		,'max'=>60
		,'required'=>'Y'
	)
	,'pass'=>array(
		'label'=>'Create a Password'
		,'max'=>60
		,'required'=>'Y'
	)
	,'user'=>array(
		'label'=>'Create a Username'
		,'max'=>60
		,'required'=>'Y'
	)
	,'email_copy'=>array(
		'label'=>'Retype E-mail Address'
		,'max'=>60
		,'required'=>'Y'
	)
	,'pass_copy'=>array(
		'label'=>'Re-enter a Password'
		,'max'=>60
		,'required'=>'Y'
	)
	,'user_copy'=>array(
		'label'=>'Re-enter Username'
		,'max'=>60
		,'required'=>'Y'
	)
);

// 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'
		,'users'
		,implode(',',$arrCols)
		,implode(',',$arrSave)
	);
	echo "<p>$strSQL</p>";
}

/*
* --------------------------------- Template -----------------------------------
*/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8">
	<title>Form</title>
	<style type="text/css">
		form fieldset input.invalid {
			border: 1px solid red;
		}
	</style>
</head>
<body>
<?php
// 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');
		}
	
		printf(
			'<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>'
			 ,str_replace('_','-',$strField)
			 ,($intUser+1)
			 ,htmlentities($arrConfig['label'])
			 ,strcmp($arrConfig['required'],'Y') == 0?'<span class="required">*</span>':''
			 ,in_array($strField,array('pass','pass_copy'))?'password':'text'
			 ,$strBaseKey
			 ,$intUser
			 ,$strField
			 ,$arrConfig['max']
			 ,$arrUser[$strField]
			 ,isset($arrErrors[$intUser][$strField])?'class="invalid"':''
			 //,isset($arrErrors[$intUser][$strField])?"<p>{$arrErrors[$intUser][$strField]}</p>":''
		);
		
		if(in_array($strField,array('user','user_copy'))) {
			echo '</div>';
		}
	}
	echo '</fieldset>';
}

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