views:

83

answers:

3

Is there ever a case (like some quirky browser) where a form input field's id becomes the key in $_POST, $_GET or $_REQUEST, instead of the field's name?

We were having trouble with a field where the ID didn't match the name. Changing the ID to match appeared to fix the problem. The issue was purely PHP parsing; no JS involved.

Coincidence?

Google returns no such thing...


A function (sorry, old php4 code) generates the field. Here's part of it

echo "<select name=\"$varName";
echo "_dd\" id=\"$varName";
echo "e_dd\">\n"; 

Removing the 'e' from that last line apparently fixed it. I didn't do it myself; someone here told me it fixed the issue. It didn't break every date field, it seemed to be an intermittent problem. That's why I immediately thought of some strange browser quirk.

I've never seen such a thing before, so I was hoping someone here has.

Thanks!

A: 

No, it’s the name attribute that names the control field:

A control's "control name" is given by its name attribute. The scope of the name attribute for a control within a FORM element is the FORM element.

Gumbo
I think what he's saying is that for some reason that appears to be *not* the case
fredley
@fredley that case as far as I know is impossible unless he has some javascript messing with his element attributes. But he did say "purely PHP"
Chris
@Gumbo: Your answer covers only HTML 4. See hopeseekr's answer.
jmz
@jmz: hopeseekr is wrong.
Gumbo
A: 

Clean code is nice code :). Can you verify this doesn't work?

echo "<select name='".$varName."_dd' id='".$varName."e_dd'>\n"; 

It would be good to see the generated html too.

fredley
`echo '<select name="'.$varName.'_dd" id="'.$varName.'e_dd">\n';` is faster, as PHP will not have to 'parse' the double quoted string. Also looks better I think.
JoostK
@JoostK: The code needs to be parsed anyways. But your example will print `\n` literally.
Gumbo
I've never been able to experience the error, so I couldn't really test it. I'm quite aware that it's not optimal... It's probably 10 years old and was written by a designer. Its working now, but I'll clean it up anyway. Thanks!
Travis
The fastest method is `echo '<select name="',$varName,'_dd" id="',$varName,'e_dd">',"\n";` :)
fredley
The code has to be parsed indeed, but with single quotes the string won't be parsed (as in `$variables` won't be parsed). You're right about the `\n`, should be `'<select name="'.$varName.'_dd" id="'.$varName.'e_dd">' . PHP_EOL;`
JoostK
@JoostK: It doesn’t matter. The parser/tokenizer has to look at each character anyways. The tokens of both, `"foo $var bar"` and `'foo '.$var.' bar'`, are something like *T\_ENCAPSED\_AND\_WHITESPACE*, *T\_VARIABLE*, *T\_ENCAPSED\_AND\_WHITESPACE*.
Gumbo
@Gumbo: In all of these examples the slowdown comes from dealing with concatenating Strings and variables before outputting them. `echo 'string',var,'string';` is the best surefire way of speeding up echo (which is essential as it's such an *annoying* bottleneck.)
fredley
@fredley: Yes, exactly.
Gumbo
+1  A: 

In XHTML 1.1 strict, id has replaced name and name is deprecated.

If your XHTML is sent using the HTTP Content-Type application/xhtml+xml (which according to standards, it must), then it is probable that a browser that goes by the standard to a Tee would use ids to populate $_POST in PHP, not name.

http://www.codingforums.com/archive/index.php/t-29229.html

See the XHTML 1.1 spec: http://www.w3.org/TR/xhtml-modularization/abstract_modules.html

Name attributes are only allowed in a, applet, frame, iframe and map elements. And any other element that just happens to have a name, must have an id of the same name.

hopeseekr
You are wrong. The `name` attribute is only deprecated for `a`, `applet`, `form`, `frame`, `iframe`, `img`, and `map`. (See http://www.w3.org/TR/xhtml1/#h-4.10)
Gumbo
You do realize that you linked to the XHTML 1.0 document. Not 1.1... http://www.w3.org/TR/xhtml-modularization/abstract_modules.htmlIt says that name="" is only to be used in frame, applet, a, iframe, and map. NOT <input> fields.It further states that in XHTML 1.1, whenever you have an element with both name and id: "if the name attribute is defined for an element, the id attribute must also be defined as the same".
hopeseekr