|
Posted by Mason Barge on 09/30/39 12:00
I have a standard POST form consisting of two types of input: text input and
textarea. The form downloads current settings from a mysql database. The
user can update the information by modifying the text and clicking a
standard "submit" button.
MAIN PROBLEM:
My problem is that the information transmitted to the formhandler apparently
has some sort of whitespace added to it. If I simply use trim() on the POST
variable, it fails the regex. If I convert to POST variable to a single
variable and use trim(), it solves the problem.
I can just convert and trim every variable, but it would be a lot cleaner to
trim the entire POST array with a foreach(). Plus, I'd much rather
understand the problem. In other words:
This code works:
$zip=trim($_POST['zip']);
if (preg_match("/^[\d]{5}/", $zip)) {
$update.=" zip='$zip',";
} else {
error_alert('Zip code must be exactly 5 digits with no other characters. ');
}
This code throws a regex rejection:
$_POST['zip']=trim($_POST['zip']);
if (preg_match("/^[\d]{5}/", $_Post['zip'])) {
$zip = $_POST['zip'];
$update.=" zip='$zip',";
} else {
error_alert('Zip code must be exactly 5 digits with no other characters. ');
}
SECONDARY PROBLEM
Of course, my real problem is how the whitespace gets in there in the first
place. Or more accurately, why the regex would reject information from the
database. In the case of "$zip", the field is VARCHAR(5).
Here's the form entry:
$zip=$row['zip'];
.. . .
<div class="tr">
<div class="tdfl">
ZIP code (5 digit):
</div>
<div class="tdfr">
<input type="text" name="zip" size="5" maxlength="5" value="<?php
if(isset($_POST['submit'])) {
echo $_POST['zip'];
} else {
echo $zip;
} ?>
" />
</div>
</div>
Does anyone have an idea why this happens? I can work around it (with a lot
of spaghetti code) but it's unsettling to have so little understanding or
control of the transition: database->form input->Post variable->regex.
TIA :)
--
Mason Barge
Navigation:
[Reply to this message]
|