$_POST array traversing and TRIM advice plz

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
tikal
Forum Newbie
Posts: 3
Joined: Tue Apr 08, 2003 10:30 am
Location: London

$_POST array traversing and TRIM advice plz

Post by tikal »

I'm trying to write validate a form script and thought I'd start with trimming all the fields with trim($_POST['$key']);

However, I can not get it to work :(

Many thanks for any advice. Feel free to highlight any bad coding practices I've picked up :o

<snip>
while (list ($key, $val) = each ($_POST)) {
// Next line was a debug attempt. I notice that although it is before
// the trim() there are no leading spaces on the output. Why?
echo "$key => $val<br />\n";
// Another attempt to check I was using the right data. I do not
// understand why this pulls up null (I think it's null, it just returns
// nothing
echo ("Post:".$_POST['$key']."<br>");
//Remove leading and trailing spaces
$_POST['$key'] = trim($_POST['$key']); }
</snip>

[space] is a literal space char... this board's templates removes the extra spaces.

The output I get when inputting "[space][space][space]g" for a field is:
fmFirstName => g
Post:


to check the trim() has taken I traverse the $_POST array with:
<snip>
echo"<pre>";
print_r($_POST);
echo"</pre><br>";
</snip>
which outputs: [fmFirstName] =>[space][space][space]g etc
So it hasn't worked! *bah*

My script in full:

<?php
session_start();
$pageTitle="Register";
//$docRoot=$_SESSION['docRoot'];
// Below is just an include for the html up to and incl <body> tag so I
// commented it out for the forum
//require 'inc/header.php';
if($_POST['fmRegistrationsubmit']){//Submit button detected
//trim the post vars
while (list ($key, $val) = each ($_POST)) {
echo "$key => $val<br />\n";
echo ("Post:".$_POST['$key']."<br>");
$_POST['$key'] = trim($_POST['$key']);
}
// The next line worked so $_POST isn't read-only
//$_POST['fmFirstName']=trim($_POST['fmFirstName']);
echo '$_POST values: <br>';
echo"<pre>";
print_r($_POST);
echo"</pre><br>";
// ... rest of validation code
}
else{
echo"<form name=\"fmRegister\" method=\"post\" action=\"register.php\">"; //replace with $docRoot.$_SERVER['PHP_SELF'] later
echo"<table class=\"formBase\">";
echo"<tr><td colspan=\"3\" class=\"formHdr\">Registration Form</td></tr>";
echo"<tr><td>First Name:</td><td><input type=\"text\" name=\"fmFirstName\" size=\"15\" maxlength=\"30\"></td><td>(Optional)</td></tr>";
echo"<tr><td>Middle name/s:</td><td><input type=\"text\" name=\"fmMiddleName\" size=\"15\" maxlength=\"30\"></td><td>(Optional)</td></tr>";
echo"<tr><td>Last name:</td><td><input type=\"text\" name=\"fmLastName\" size=\"15\" maxlength=\"30\"></td><td>(Optional)</td></tr>";
echo"<tr><td>UserName</td><td><input type=\"text\" name=\"fmUserName\" size=\"15\" maxlength=\"30\"></td><td>&nbsp;</td></tr>";
echo"<tr><td>Password</td><td><input type=\"password\" name=\"fmPassword\" size=\"15\" maxlength=\"30\"></td><td>&nbsp;</td></tr>";
echo"<tr><td>Repeat Password</td><td><input type=\"text\" name=\"fmPasswordConfirm\" size=\"15\" maxlength=\"30\"></td><td>&nbsp;</td></tr>";
echo"<tr><td>email</td><td><input type=\"text\" name=\"fmEmail\" size=\"15\" maxlength=\"30\"></td><td>&nbsp;</td></tr>";
echo"<tr><td><input type=\"submit\" name=\"fmRegistrationsubmit\" value=\"Register\"></td><td>&nbsp; </td><td>&nbsp;</td></tr>";
echo"<tr><td colspan=\"3\">Emails are used to confirm user identity only and are<br> never disclosed or passed on to third parties.</td></tr>";
echo"</table>";
echo"</form>";
}
echo"</body></html>";
//require 'inc/footer.php';
?>
User avatar
bznutz
Forum Commoner
Posts: 58
Joined: Mon Dec 09, 2002 9:52 pm
Location: Here
Contact:

Post by bznutz »

When you say "trim($_POST['$key'];", try removing the single quotes from $key. Like this: trim($_POST[$key]);. It's the only possible culprit I can find.
tikal
Forum Newbie
Posts: 3
Joined: Tue Apr 08, 2003 10:30 am
Location: London

Post by tikal »

*slaps forehead*

Yep, that fixed it :oops:

Cheers.
User avatar
bznutz
Forum Commoner
Posts: 58
Joined: Mon Dec 09, 2002 9:52 pm
Location: Here
Contact:

Post by bznutz »

PHP is a parser, above all else. It expects all code to be very specific. :D
Post Reply