foreach problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
partiallynothing
Forum Commoner
Posts: 61
Joined: Fri Nov 21, 2003 5:02 pm
Location: connecticut, usa

foreach problem

Post by partiallynothing »

The below code works w/o errors:

Code: Select all

<?php
foreach($_POST as $varname => $value) {
	$formVars['$varname'] = trim($value);
	}	
?>
but when I try to call a variable that should have been created with it, I get an error. So, for instance, when I echo $formVars['logon'], i get an Undefined Variable Error.

My index.php page is simple:

Code: Select all

<?php
<html>
<head>
<title>PartiallyNothing</title>
</head>
<body>
<form name="logon" method="post" action="inc/common/FormHandler.php?logon=true">
  <p>
    <input name="logon" type="text" id="logon">
  </p>
  <p>
    <input name="password" type="password" id="password">
  </p>
  <p>    <input name="submit" type="submit" id="submit" value="Submit">
      </p>
</form>
</body>
</html>
?>
My form handler page looks like this:

Code: Select all

<?php
if (isset($_GET['logon'])) {

	//start a session
	session_start();
	
	//create form array
	foreach ($_POST as $varname => $value) {
		$formVars['$varname'] = trim($value);
	}
	
	//check for empty fields	
	if (empty($formVars['logon']) || empty($formVars['password'])) {
		echo 'Error: No username or password specified.';
		die();
		}
	
	//check for incorrect characters
	if (!eregi("^[a-z'-}*$", $formVars['logon']) || !eregi("^[a-z'-}*$", $formVars['password'])) {
		echo 'Error: You username and password can only contain alphebetic characters or "-" or ";"';
		die();
		}
	
	//check for length
	if (strlen($formVars['logon']) > 50 || strlen($formVars['password']) > 50) {
		echo 'Error: You username and password cannot exceed 50 characters.';
		die();
		}
	
	//encrypt password
	$formVars['password'] = md5($formVars['password'], $formVars['logon']);
	
	//connect to database
	include('inc/common/db_connect.php');
	
	//query databse
	$query = "INSERT INTO users VALUES (NULL, {$formVars['logon']}, {$formVars['password']})";
	@ mysql_query($query) or die (mysql_error());
	}
	
elseif (isset($_GET['logoff'])) {
	
	echo 'exicute logoff script';
	}
	
elseif (isset($_GET['register'])) {

	echo 'exicute register script';
	}
	
elseif (isset($_GET['updateprofile'])) {

	echo 'exicute update profile script';
	}
?>
I get back my first error: No username or password specified, so I think it's not creating the formVars array.

Can anyone figure out why this does not work?!

Thanks a lot!
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$formVars['$varname'] = trim($value); <-- try removing the single quotes otherwise PHP will evaluate it as a literal '$varname'
User avatar
partiallynothing
Forum Commoner
Posts: 61
Joined: Fri Nov 21, 2003 5:02 pm
Location: connecticut, usa

Post by partiallynothing »

that fixed it; thanks!
Post Reply