473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

$_POST adding whitespace to variable value

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

Jan 15 '08 #1
10 2979
"Mason Barge" <ma********@comcast.netwrote in
news:Nf******************************@comcast.com:
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.
No it doesn't. You are confused about what a post variable is. You
can't change what's been $_POSTed, but you can certainly ASSIGN the
value to your own variable and change *that*.
If I convert to POST
variable to a single variable and use trim(), it solves the problem.
As explained above.

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']);
as hinted at above - what are you doing here? why are you trying to
*set* a $_POST variable, which has a very specific meaning? Why are you
using this code at all, as opposed to the code above which does what you
want in the correct way???

Are you trying to trim the $_POST elements in one fail swoop? Well you
can either use array_map to apply a trim-type function to the $_POST
array, but be aware that it will mess up any $_POST elements which are
arrays themselves (unless you use one of the user contributions on the
manual page). Personally, I specifically trim every $_POST'ed element.

Of course, my real problem is how the whitespace gets in there in the
first place.
Nope, who cares how it got in?

Jan 15 '08 #2
On Tue, 15 Jan 2008 17:28:59 +0100, Mason Barge <ma********@comcast.net>
wrote:
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.

This code throws a regex rejection:

$_POST['zip']=trim($_POST['zip']);
if (preg_match("/^[\d]{5}/", $_Post['zip'])) {
$_Post != $_Post
As soon as you've fixed that, this would work, but it is not
recommendable. Having a clear view of what your user actually submitted,
and how you've handled/altered the data after that will save you lots of
debugging time.

And just on a side note: shouldn't it be "/^[\d]{5}$/" ? The regex now
will consider '12345and then some arbitrary text or characters' valid...
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:
<input type="text" name="zip" size="5" maxlength="5"
value="<?php if(isset($_POST['submit'])) {
echo $_POST['zip'];
} else {
echo $zip;
} ?>
" />
Here is is. You have whitespace between '?>' and '" />', which will
probably translate itself to a space in the field in HTML context.
Use:' } ?>" />'

--
Rik Wasmus
Jan 15 '08 #3

"Good Man" <he***@letsgo.comwrote in message
news:Xn************************@216.196.97.131...
"Mason Barge" <ma********@comcast.netwrote in
news:Nf******************************@comcast.com:
>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.

No it doesn't. You are confused about what a post variable is. You
can't change what's been $_POSTed, but you can certainly ASSIGN the
value to your own variable and change *that*.
>If I convert to POST
variable to a single variable and use trim(), it solves the problem.

As explained above.

>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']);

as hinted at above - what are you doing here? why are you trying to
*set* a $_POST variable, which has a very specific meaning? Why are you
using this code at all, as opposed to the code above which does what you
want in the correct way???

Are you trying to trim the $_POST elements in one fail swoop? Well you
can either use array_map to apply a trim-type function to the $_POST
array, but be aware that it will mess up any $_POST elements which are
arrays themselves (unless you use one of the user contributions on the
manual page). Personally, I specifically trim every $_POST'ed element.
Thanks for the response, it was very helpful.

Jan 15 '08 #4
On Tue, 15 Jan 2008 18:06:21 +0100, Good Man <he***@letsgo.comwrote:
>Of course, my real problem is how the whitespace gets in there in the
first place.

Nope, who cares how it got in?
Euhm, if I request a form editing some record, and I change one field, I
don't expect to get an 'invalid' error on another in my view unaltered
field. In this case, we KNOW whitespace is invalid, and will trim() it
out. However, if whitespace is valid, this could stack whitespace on the
end for every edit action. Not something you'd like.
--
Rik Wasmus
Jan 15 '08 #5
"Rik Wasmus" <lu************@hotmail.comwrote in
news:op***************@metallium.lan:
On Tue, 15 Jan 2008 18:06:21 +0100, Good Man <he***@letsgo.comwrote:
>>Of course, my real problem is how the whitespace gets in there in
the first place.

Nope, who cares how it got in?

Euhm, if I request a form editing some record, and I change one field,
I don't expect to get an 'invalid' error on another in my view
unaltered field. In this case, we KNOW whitespace is invalid, and
will trim() it out. However, if whitespace is valid, this could stack
whitespace on the end for every edit action. Not something you'd
like.
true, i wasn't even imagining it being created on the back-end/server-side.
Jan 15 '08 #6
On Tue, 15 Jan 2008 18:26:12 +0100, Good Man <he***@letsgo.comwrote:
"Rik Wasmus" <lu************@hotmail.comwrote in
news:op***************@metallium.lan:
>On Tue, 15 Jan 2008 18:06:21 +0100, Good Man <he***@letsgo.comwrote:
>>>Of course, my real problem is how the whitespace gets in there in
the first place.

Nope, who cares how it got in?

Euhm, if I request a form editing some record, and I change one field,
I don't expect to get an 'invalid' error on another in my view
unaltered field. In this case, we KNOW whitespace is invalid, and
will trim() it out. However, if whitespace is valid, this could stack
whitespace on the end for every edit action. Not something you'd
like.

true, i wasn't even imagining it being created on the
back-end/server-side.
The OP didn't mention it indeed. I assumed it because the zip code field
apparantly was filled with a $row['zip'] variable, which makes one
immediately think of a fetched row from a database result.
--
Rik Wasmus
Jan 15 '08 #7

"Rik Wasmus" <lu************@hotmail.comwrote in message
news:op***************@metallium.lan...
On Tue, 15 Jan 2008 18:26:12 +0100, Good Man <he***@letsgo.comwrote:
>"Rik Wasmus" <lu************@hotmail.comwrote in
news:op***************@metallium.lan:
>>On Tue, 15 Jan 2008 18:06:21 +0100, Good Man <he***@letsgo.comwrote:
Of course, my real problem is how the whitespace gets in there in
the first place.

Nope, who cares how it got in?

Euhm, if I request a form editing some record, and I change one field,
I don't expect to get an 'invalid' error on another in my view
unaltered field. In this case, we KNOW whitespace is invalid, and
will trim() it out. However, if whitespace is valid, this could stack
whitespace on the end for every edit action. Not something you'd
like.

true, i wasn't even imagining it being created on the
back-end/server-side.

The OP didn't mention it indeed.
"The form downloads current settings from a mysql database."

;^)
Jan 15 '08 #8
On Tue, 15 Jan 2008 19:04:51 +0100, Steve <no****@example.comwrote:
"Rik Wasmus" <lu************@hotmail.comwrote in message
news:op***************@metallium.lan...
>On Tue, 15 Jan 2008 18:26:12 +0100, Good Man <he***@letsgo.comwrote:
>>"Rik Wasmus" <lu************@hotmail.comwrote in
news:op***************@metallium.lan:
On Tue, 15 Jan 2008 18:06:21 +0100, Good Man <he***@letsgo.comwrote:
>Of course, my real problem is how the whitespace gets in there in
>the first place.
>
Nope, who cares how it got in?

Euhm, if I request a form editing some record, and I change one field,
I don't expect to get an 'invalid' error on another in my view
unaltered field. In this case, we KNOW whitespace is invalid, and
will trim() it out. However, if whitespace is valid, this could stack
whitespace on the end for every edit action. Not something you'd
like.

true, i wasn't even imagining it being created on the
back-end/server-side.

The OP didn't mention it indeed.

"The form downloads current settings from a mysql database."
Ah, carefull reading code, less carefull reading introductions, my bad :).
--
Rik Wasmus
Jan 15 '08 #9

"Rik Wasmus" <lu************@hotmail.comwrote in message
news:op***************@metallium.lan...
On Tue, 15 Jan 2008 17:28:59 +0100, Mason Barge <ma********@comcast.net>
wrote:
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.

This code throws a regex rejection:

$_POST['zip']=trim($_POST['zip']);
if (preg_match("/^[\d]{5}/", $_Post['zip'])) {
$_Post != $_Post
As soon as you've fixed that, this would work, but it is not
recommendable. Having a clear view of what your user actually submitted,
and how you've handled/altered the data after that will save you lots of
debugging time.

And just on a side note: shouldn't it be "/^[\d]{5}$/" ? The regex now
will consider '12345and then some arbitrary text or characters' valid...

Yes, of course it should. I got sloppy because the input form is maximum
size 5.
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:
<input type="text" name="zip" size="5" maxlength="5"
value="<?php if(isset($_POST['submit'])) {
echo $_POST['zip'];
} else {
echo $zip;
} ?>
" />
Here is is. You have whitespace between '?>' and '" />', which will
probably translate itself to a space in the field in HTML context.
Use:' } ?>" />'

--
Rik Wasmus

That's so dumb I can't even get embarrassed. Even worse, I've fixed the
exact problem before, LOL. Thanks for pointing it out.

I think I need a nap.

Jan 15 '08 #10

"Rik Wasmus" <lu************@hotmail.comwrote in message
news:op***************@metallium.lan...
On Tue, 15 Jan 2008 19:04:51 +0100, Steve <no****@example.comwrote:
>"Rik Wasmus" <lu************@hotmail.comwrote in message
news:op***************@metallium.lan...
>>On Tue, 15 Jan 2008 18:26:12 +0100, Good Man <he***@letsgo.comwrote:
"Rik Wasmus" <lu************@hotmail.comwrote in
news:op***************@metallium.lan:
On Tue, 15 Jan 2008 18:06:21 +0100, Good Man <he***@letsgo.comwrote:
>>Of course, my real problem is how the whitespace gets in there in
>>the first place.
>>
>Nope, who cares how it got in?
>
Euhm, if I request a form editing some record, and I change one field,
I don't expect to get an 'invalid' error on another in my view
unaltered field. In this case, we KNOW whitespace is invalid, and
will trim() it out. However, if whitespace is valid, this could stack
whitespace on the end for every edit action. Not something you'd
like.

true, i wasn't even imagining it being created on the
back-end/server-side.

The OP didn't mention it indeed.

"The form downloads current settings from a mysql database."

Ah, carefull reading code, less carefull reading introductions, my bad :).
i'm just mess'n with you rik. even without, you still divined the correct
answer. :)
Jan 15 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jack | last post by:
How would I add a variable that I will assign to a list of $_POST variables that I extract from a form? My form passes a value for $q. That works fine. What I want to do is run an if/else on it...
7
by: Dan | last post by:
I was trying to troubleshoot a login page that doesn't work - it keeps saying the login/password is missing - when my tracing discovered this peculiar behavior. register_globals is off, so at...
10
by: arun.kumar.varma | last post by:
Hi, I'm learning PHP and prepared a simple login form. validate.php does the validation and it was behaving erratically. So, I did var_dump of $_POST variable and it's NULL. Did I miss anything...
15
by: zorro | last post by:
greetings... I'm wondering what more advanced coders would think ot this: $_POST = clean($_POST); and now I can use POST directly: $sql= "select * from T1 where myvar='$_POST' " ;
7
by: Dynamo | last post by:
I am using values stored an $_POST array to display records from a table before asking the user if he is sure he wants to delete them. If the user confirms then the records are deleted. Without...
17
by: john | last post by:
All: I'm a long-time developer, new to PHP.... Is there an idiom used in PHP to construct SQL statments from $_POST data? I would guess that in many applications, the data read from $_POST...
2
by: keeps21 | last post by:
I have a script that recieves an id number via the address bar when a link is clicked. ie . index.php?id=1 if the link was for the story whose ID is 1. My script checks if a user is logged in,...
7
by: lawpoop | last post by:
Hello all - Is there a way to get a nested array in a $_POST variable? I have a form where there are several questions, each one corresponding to a database row. On submission of the form, I...
5
by: John Gordon | last post by:
My XSLT files have many occurrences of this general pattern: <a> <xsl:attribute name="href"> <xsl:value-of select="xyz" /> </xsl:attribute> </a> When I execute an XSL transform, the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.