Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 142
  • Last Modified:

getting the contents of a form

I have a form with a input field the unique thing with this input field is that the name contains a space in between.
<INPUT name="first name">
NOw if i have a space in between the name i cannot fetch the value of this input field on the next page.
Sample code in PHP. You can try any language
<HTML>
<HEAD><TITLE></TITLE></HEAD>
<BODY bgColor=lightgoldenrodyellow>
<form name="test" action="try3.php" method="POST">
<TABLE cellSpacing=0 cellPadding=0 width="75%" border=0>
  <TR>
    <TD>Name</TD>
    <TD><INPUT name="first name"><BR></TD>
  </TR>
  </TABLE></P>
<P>&nbsp;</P>
<input name=submit value=Send type=submit>
</form>
</BODY>
</HTML>

try3.php
<HTML>
<HEAD><TITLE></TITLE></HEAD>
<BODY bgColor=lightgoldenrodyellow>
<?php
$s = trim($_POST['first name']);
echo $s;
//$s = trim($_POST['first+name']);
//echo $s;
?>

</BODY>
</HTML>

Thank You
0
chopra_rr
Asked:
chopra_rr
  • 8
  • 6
  • 4
  • +4
1 Solution
 
dorwardCommented:
Solution:
Don't put a space in the name, if you want a seperator use an underscore of camal case!

<INPUT name="first_name">
<INPUT name="firstName">

Reason:
Control names are not allowed to contain spaces in HTML.

From the HTML specification:
http://www.w3.org/TR/html4/types.html#type-cdata

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 (".").

0
 
mythreyivCommented:


Hi chopra,

while giving  html names u shouldnot put space between them ,use any seperator if you want.
but you can use genral JAVA convention

java variable names start with small letter any break occures  we have to put the capitla letter
so firt name can me modified as
  firstName.

u can follow this convention
or you can use some seperator first-name like this.

Mythreyi
0
 
knightEknightCommented:
I don't know anything about PHP ... but can you create a string variable and use that instead?

str = 'first name';
$s = trim($_POST[str]);


or maybe:  str = 'first%20name';
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dorwardCommented:
knightEknight: No, because you can't have spaces in the name in the first place.
0
 
CyberGhostCommented:
Why doing things simmilar if they can be done hard, no? It is not recommended (and not not functional in your case) to use spaces in variable name. You can see it on your own. Just put "_" instead of space to that variable and it will be fully accessible.

regards
0
 
dorwardCommented:
Not recomended? It is FORBIDDEN to use spaces in names of form elements. Not "It's a bad idea", but "You must not do it."
0
 
CyberGhostCommented:
Oh sorry... I wanted to say is softly. Think I missed the idea ;o)
0
 
knightEknightCommented:
I assume you mean it is forbidden when using PHP.
0
 
drnCommented:
just use the underscore ( _ )
0
 
dorwardCommented:
knightEknight: No, it is forbidden when using HTML (that's why I quoted the HTML spec in my inital comment). It doesn't matter what you process the data with at the back end. HTML form elements can not have spaces in the name.
0
 
knightEknightCommented:
I disagree on a technicality only.  I agree that it is not good practice and not straight with the HTML specs -- but it can be done.  I don't know if it will work with PHP, but it certainly works with javascript and ASP:


<HTML>
<BODY>
<FORM name='myform' method='post' action='mytest.asp'
      onsubmit='alert(this.elements["test 2"].value);return true;' >
<INPUT type='text' name='test1' value='test1' />
<INPUT type='text' name='test 2' value='test 2' />
<INPUT type='submit' />
</FORM>
</BODY>
</HTML>



then in mytest.asp:

<HTML>
<BODY>
<BR /><%=request("test1")%>
<BR /><%=request("test 2")%>
</BODY>
</HTML>


I'm quite certain it will work with JSP and CGI as well.  

Anyway, I'm not trying to start an esoteric argument, I just think that if it can be done in these other technologies, perhaps it can be done in PHP as well -- I just don't know how.

0
 
knightEknightCommented:
( "test 2" has a space in it above -- "test1" does not. )
0
 
dorwardCommented:
It can be done? So you've tested it, and every browser accepts the invalid input?
0
 
knightEknightCommented:
well ... I don't know of a browser that accepts everything.  But certainly the latest versions of the most popular browsers will work with the code above -- as tested.  :>)
0
 
knightEknightCommented:
I know that the above works with IE4+ and NS4+.
0
 
CyberGhostCommented:
W3C (World Wide Web Consortium):
www.w3c.org (international organization that certify HTML and other web standards) has accepted this variant:

"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 (".")."

... there is no word about it can be followed by " " (space). So maybe this will solve your "technical" problem.

regards
0
 
CyberGhostCommented:
Sorry u guyz... I've missed that you wrote it above.
0
 
knightEknightCommented:
yes, I accept that -- but that doesn't change the fact that if you put spaces in the element names, it still works :)
0
 
knightEknightCommented:
but you must quote the name in the tag:

   <INPUT  name="test  2"  type="text"  ...

instead of:

   <INPUT  name=test  2  type=text   ...
0
 
msdickersonCommented:
I think the problem here is that most parsers used to get POST data will use a space as a seperater.  This of course causes problems like what was seen.  Some languages can get around it and some can't.  Variable names with spaces will cause problems depending how they are parsed.  Perl and PHP by default tend to use spaces as string delimiters, but can be reconfigured to use something else.  JSP, Javascript, and other languages can be manipulated also to use something else as delimiters.  CGI is not a language so it has nothing specific to it, any language can be a CGI so long as it supports the appropriate IO.  I am not a Microsoft programmer, so will not comment on ASP and .net languages.  


I simply think the best bet is to make the variable names not have spaces as earlier suggested - Unix world people like the "_"  character and old mainframers like the "-".  Or just clump the names together with no delimiters.  

If you need definition for a user interface as in your example:
<TD><INPUT name="first name"><BR></TD>
Then I would code it something like this:
<TD>First Name<INPUT name="first_name"><BR></TD>


Good luck
0
 
COBOLdinosaurCommented:
This question has been classified abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.

<note>
Unless it is clear to me that the question has been answered I will recommend delete.  It is possible that a Grade less than A will be given if no expert makes a case for an A grade. It is assumed that any participant not responding to this request is no longer interested in its final disposition.
</note>

If the user does not know how to close the question, the options are here:
http://www.experts-exchange.com/help/closing.jsp


Cd&

0
 
dorwardCommented:
recommendation: Points to me. Problem explained. Solution suggested. Evidence provided.
0
 
COBOLdinosaurCommented:
interesting thread.  Points for the "technically correct" solution

It is time to clean this abandoned question up.

I am putting it on a clean up list for CS.

<recommendation>
points to dorward

</recommendation>

If anyone participating in the Q disagrees with the recommendation,
please leave a comment for the mods.

Cd&

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 6
  • 4
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now