473,769 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

$_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($_POS T['zip']);
if (preg_match("/^[\d]{5}/", $zip)) {
$update.=" zip='$zip',";
} else {
error_alert('Zi p 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('Zi p 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 3014
"Mason Barge" <ma********@com cast.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($_POS T['zip']);
if (preg_match("/^[\d]{5}/", $zip)) {
$update.=" zip='$zip',";
} else {
error_alert('Zi p 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********@com cast.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.c omwrote in message
news:Xn******** *************** *@216.196.97.13 1...
"Mason Barge" <ma********@com cast.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($_PO ST['zip']);
if (preg_match("/^[\d]{5}/", $zip)) {
$update.=" zip='$zip',";
} else {
error_alert('Z ip 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.c omwrote:
>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.comwro te in
news:op******** *******@metalli um.lan:
On Tue, 15 Jan 2008 18:06:21 +0100, Good Man <he***@letsgo.c omwrote:
>>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.c omwrote:
"Rik Wasmus" <lu************ @hotmail.comwro te in
news:op******** *******@metalli um.lan:
>On Tue, 15 Jan 2008 18:06:21 +0100, Good Man <he***@letsgo.c omwrote:
>>>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.comwro te in message
news:op******** *******@metalli um.lan...
On Tue, 15 Jan 2008 18:26:12 +0100, Good Man <he***@letsgo.c omwrote:
>"Rik Wasmus" <lu************ @hotmail.comwro te in
news:op******* ********@metall ium.lan:
>>On Tue, 15 Jan 2008 18:06:21 +0100, Good Man <he***@letsgo.c omwrote:
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.comwro te in message
news:op******** *******@metalli um.lan...
>On Tue, 15 Jan 2008 18:26:12 +0100, Good Man <he***@letsgo.c omwrote:
>>"Rik Wasmus" <lu************ @hotmail.comwro te in
news:op****** *********@metal lium.lan:
On Tue, 15 Jan 2008 18:06:21 +0100, Good Man <he***@letsgo.c omwrote:
>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.comwro te in message
news:op******** *******@metalli um.lan...
On Tue, 15 Jan 2008 17:28:59 +0100, Mason Barge <ma********@com cast.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

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

Similar topics

10
2669
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 and assign a new variable based on what was chosen such as if ($q == "red") { $q = "tp"; //change $q to this value $sub = "ak"; //attach this value to the $sub variable } else {
7
11879
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 the top of my script I assign a few variables to incoming GET and POST values. $login = clean($_POST, 30); $passwd = clean($_POST, 30);
10
14696
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 here regarding the configuration or code? Code for validate.php is given below. <?php var_dump($_POST);
15
7151
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
2491
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 boring you with all of the code here is the rough idea. <?php $delete = '( id = ' . implode(' OR id = ', $_POST ) . ' ) '; if (!isset($_POST)) { // Then display the records that were marked for deletion ?>
17
4445
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 are used to build SQL statements. Certainly, we can do the following:
2
3142
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, if not they are redirected to the login page. If logged in they may edit the story. I assign $_GET to $id.
7
5200
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 loop through the $_POST array, and update the database appropriately. So my post variable looks like this:
5
3909
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 resulting HTML looks like this:
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.