Blank in name attribute converted to underscore

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

User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Blank in name attribute converted to underscore

Post by anjanesh »

Hi

Whitespaces in input's name attribute are getting converted to underscores.

Code: Select all

<input type="text" name="Full Name"/>

Code: Select all

print_r($_POST);
Output

Code: Select all

Array ( [Full_Name] => Some Name................)
How long has this been like this ?

Thanks

[FireFox 1.5.0.4, PHP 5.1.4, Apache 2.0.58]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Why are there spaces in your field names?

To be back on topic, I had no idea that form fields with spaced names were modified during post.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Everah wrote:Why are there spaces in your field names?
I want them to match exactly as my MySQL fieldnames.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

MySQL field names shouldn't have spaces either.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I want them to match exactly as my MySQL fieldnames.
And why are there spaces in your db field names? :lol:
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Because MySQL handles it well allowing us to define fieldnames as proper names. I've always used whitespaces in MySQL fieldnames from the start.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I would recommend not using spaces in our DB field names. And I would recommend not using spaces in your form field names.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

...there's always str_replace(). Image

I would hazard to guess it's Firefox that's adding the underscore.. or it may be your own code somewhere. Darn near impossible to say right now.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

recent versions of MySQL allows unicode in field names. Yet I don't store my metadata in Russian... guess why.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

feyd wrote:I would hazard to guess it's Firefox that's adding the underscore.. or it may be your own code somewhere. Darn near impossible to say right now.
post-test.php

Code: Select all

<?php
print_r($_POST);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr">
<head>
<title>POST Test</title>
</head>
<body>
<form action="" method="post">
Full Name : <input type="text" name="Full Name"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
On both my localhost and server using FF 1.5.0.4 and IE 7 beta 2, the whitespace gets converted to underscore.
If converted to GET, it becomes post-test.php?Full+Name=someone but print_r($_GET) still prints Array ( [Full_Name] => someone )

Changing the PHP code to ASP .NET and running it on IIS 5.1 (post-test.aspx)

Code: Select all

<%
Response.Write(Request.Form())
%>
I get Full+Name=someone as output.

So Im guessing this is Apache's doing ? Im yet to try this on PHP+IIS.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

So Im guessing this is Apache's doing ?
As a quick check, do var_dump($_SERVER["QUERY_STRING"]);
If there are underscores, then that's apache thing, otherwise it's php's behaviour
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Might need to check $HTTP_RAW_POST_DATA too.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I'm still thinking it may be easier (and better) to axe the spaces in your db field names and form field names.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Everah wrote:I'm still thinking it may be easier (and better) to axe the spaces in your db field names and form field names.
I would agree, but so far, anjanesh has disliked that solution.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

feyd wrote:
Everah wrote:I'm still thinking it may be easier (and better) to axe the spaces in your db field names and form field names.
I would agree, but so far, anjanesh has disliked that solution.
Yeah, I know. And I suppose repeating that solution makes me look like a persistent @$$, so, with that, I believe I will be vacating my position in this thread.
Post Reply