martinclewett Posted December 7, 2007 Share Posted December 7, 2007 Hello, I've just starting using PHP having moved from ASP because ASP.NET is bollox! Anyway I've got a feeling this is an unsolvable problem but here it is anyway. I want to have spaces in my input box names in HTML. i.e. <input type="text" name="This is the name">. I don't think this is an unreasonable request. Anyway when reading the keys in the $_POST associative array on submission the spaces have changed to underscores! Highly annoying. At first I thought it might be a limitation of associative arrays but apparently I can have spaces in the keys. Here is an example of what I'm talking about (I'm using version 5.2.4 of php): --- stupid.php --- <html> <head> <?php if(isset($_POST)){ //Get the information from the $_POST array foreach($_POST as $posted_key=>$posted_value){ echo($posted_key.' <---------- why have the spaces changed to underscores????????!!!!!<br>'.chr(13).chr(10)); } //What happened to the spaces? //The following works right! $post['This is the name of the text box']='This is the value of the textbox, but I don\'t care about that!'; $post['This is name of the submit button']='Submit'; foreach($post as $posted_key=>$posted_value){ echo($posted_key.' <---------- this works!<br>'.chr(13).chr(10)); } } ?> </head> <body> <form action="stupid.php" method="POST"> <input type="text" name="This is the name of the text box" value="This is the value of the textbox, but I don't care about that!"> <input type="submit" name="This is name of the submit button" value="Submit"> </form> </body> </html> ------ I am writing an application that takes the textbox name from a database column name, which can contain spaces. It is highly annoying if PHP just won't do this. Especially if you have two columns with names "column_a" and "column a", unlikely I know but my database is user defined so god knows what some freak with type in as a column name. There are obviously work arounds but I need to know that php will definitely not do what I want it to first, before exploring them. Any help would be greatly appreciated. Martin Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted December 7, 2007 Share Posted December 7, 2007 try this foreach($_POST as $posted_key=>$posted_value){ $posted_key = str_replace("_", " ", $posted_key); echo($posted_key.' '.chr(13).chr(10)); } http://php.net/str_replace Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 7, 2007 Share Posted December 7, 2007 Variable names cannot contain spaces (or dots.) Just alphanumeric characters and underscores. Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted December 7, 2007 Share Posted December 7, 2007 ^ I know var names can't contain spaces.. but I thought that array index could $_POST["i have spaces"]; Quote Link to comment Share on other sites More sharing options...
martinclewett Posted December 7, 2007 Author Share Posted December 7, 2007 foreach($_POST as $posted_key=>$posted_value){ $posted_key = str_replace("_", " ", $posted_key); echo($posted_key.' '.chr(13).chr(10)); } This will not work if the inputbox name contains both underscores and spaces. Variable names cannot contain spaces (or dots.) Just alphanumeric characters and underscores. Are associative array keys bound by the same rules as variable names, they don’t seem to be (see my example)? Martin Quote Link to comment Share on other sites More sharing options...
martinclewett Posted December 7, 2007 Author Share Posted December 7, 2007 I've been carefully reading http://www.w3.org/TR/html4/interact/forms.html#h-17.4 http://www.w3.org/TR/html4/types.html#type-cdata. which say: ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). Browsers don't seem to care about what you put in your name="........." but maybe the PHP boys do care. To test this hypothesis I wrote the following script which explicitly uses all the characters I could find that are not allowed by the W3 definition above. <html> <head> <?php if(isset($_POST)){ //Get the information from the post array foreach($_POST as $posted_key=>$posted_value){ if($posted_key!=$posted_value){ echo($posted_key.' should be '.$posted_value.'!<br>'.chr(13).chr(10)); } else{ echo($posted_key.' works!<br>'.chr(13).chr(10)); } } } ?> </head> <body> <form action="stupid.php" method="POST"> <input type="text" name="abc!def" value="abc!def"> <input type='text' name='abc"def' value='abc"def'> <input type="text" name="abc£def" value="abc£def"> <input type="text" name="abc$def" value="abc$def"> <input type="text" name="abc%def" value="abc%def"> <input type="text" name="abc^def" value="abc^def"> <input type="text" name="abc&def" value="abc&def"> <input type="text" name="abc*def" value="abc*def"> <input type="text" name="abc(def" value="abc(def"> <input type="text" name="abc)def" value="abc)def"> <input type="text" name="abc+def" value="abc+def"> <input type="text" name="abc=def" value="abc=def"> <input type="text" name="abc{def" value="abc{def"> <input type="text" name="abc}def" value="abc}def"> <input type="text" name="abc[def" value="abc[def"> <input type="text" name="abc]def" value="abc]def"> <input type="text" name="abc;def" value="abc;def"> <input type="text" name="abc'def" value="abc'def"> <input type="text" name="abc@def" value="abc@def"> <input type="text" name="abc#def" value="abc#def"> <input type="text" name="abc~def" value="abc~def"> <input type="text" name="abc<def" value="abc<def"> <input type="text" name="abc>def" value="abc>def"> <input type="text" name="abc,def" value="abc,def"> <input type="text" name="abc?def" value="abc?def"> <input type="text" name="abc/def" value="abc/def"> <input type="text" name="abc\def" value="abc\def"> <input type="text" name="abc|def" value="abc|def"> <input type="text" name="abc`def" value="abc`def"> <input type="text" name="abc¬def" value="abc¬def"> <input type="text" name="abc def" value="abc def"> <input type="submit" name="sub"> </form> </body> </html> I got the following output: abc!def works! abc"def works! abc£def works! abc$def works! abc%def works! abc^def works! abc&def works! abc*def works! abc(def works! abc)def works! abc+def works! abc=def works! abc{def works! abc}def works! abc_def should be abc def! abc]def works! abc;def works! abc'def works! abc@def works! abc#def works! abc~def works! abc abc>def works! abc,def works! abc?def works! abc/def works! abc\def works! abc|def works! abc`def works! abc¬def works! So if that was their reasoning then they haven't been very consistent about it. Space is still the only W3 illegal character that won’t work with PHP. Also I've performed another test to see if any of the legal characters mentioned in the W3 definition got messed up by PHP in the same way. The only one I could find was dot: <html> <head> <?php if(isset($_POST)){ //Get the information from the post array foreach($_POST as $posted_key=>$posted_value){ if($posted_key!=$posted_value){ echo($posted_key.' should be '.$posted_value.'!<br>'.chr(13).chr(10)); } else{ echo($posted_key.' works!<br>'.chr(13).chr(10)); } } } ?> </head> <body> <form action="stupid.php" method="POST"> <input type="text" name="abc.def" value="abc.def"> <input type="submit" name="sub"> </form> </body> </html> So in summary PHP seems to mess up dots which are perfectly legal according to W3, and implements illegal characters: ! " £ $ % ^ & * ( ) + = { } ] ; ' @ # ~ > , ? / \ | ` ¬ Possibly this is because PHP variable names can not contain spaces or dots but this statement only refers to PHP variable names and not HTML form element names as defined by W3. Therefore the omission of the dot at least seems like a definite bug. But as far as I’m concerned I think that anything put into name=”…….” should come out unchanged on the other side. Martin Quote Link to comment Share on other sites More sharing options...
martinclewett Posted December 7, 2007 Author Share Posted December 7, 2007 OK, I have done some more research and I still can't see a valid reason for not including the dot. I have concocted a work around that builds an associative array from the raw posted data. It works on all the examples I have previously given but you may need to set always_populate_raw_post_data to be on in your php.ini file. It uses the $GLOBALS['HTTP_RAW_POST_DATA'] variable and converts it using urldecode(...). The function outputs an associative array that works better than $_POST! Why they don't just implement something like this in PHP I don't know, maybe they have and I've just reinvented the wheel, I don't know. function get_posted_data_from_raw(){ //For this to work the always_populate_raw_post_data configuration directive in php.ini need to be on if(isset($GLOBALS['HTTP_RAW_POST_DATA'])){ $temparr1 = split('&',$GLOBALS['HTTP_RAW_POST_DATA']); foreach($temparr1 as $tempstr1){ $temparr2 = split('=',$tempstr1); $post[urldecode($temparr2[0])] = urldecode($temparr2[1]); } return $post; } } Martin Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.