1

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.

6
  • 4
    Some code and examples would be useful? Are you using JavaScript to submit the form? Commented Aug 26, 2010 at 14:28
  • 1
    @Travis Paste some code. Commented Aug 26, 2010 at 14:33
  • Absolutely no JS, just a submit button. The field in question is a date field with three drop-downs named like (field_yyyy, field_dd, field_mm). On the receiving end, a PHP function goes through $_POST, recognizes that there's a date, concatenates them in the appropriate format, escapes them, and enters them into the database. Commented Aug 26, 2010 at 14:33
  • The browser generates a POST or a GET request, PHP handles this request and manages those POST/GET variables in the $_POST and $_GET arrays. Your problem can't be caused by PHP, only the browser, that's the one generating the request. You can debug the request with e.g. FireBug. Commented Aug 26, 2010 at 14:33
  • I wonder if there's a browser quirk that's causing it... What browser is this occurring in (Have you tried multiple different browsers)? Commented Aug 26, 2010 at 14:36

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You are wrong. The name attribute is only deprecated for a, applet, form, frame, iframe, img, and map. (See w3.org/TR/xhtml1/#h-4.10)
You do realize that you linked to the XHTML 1.0 document. Not 1.1... w3.org/TR/xhtml-modularization/abstract_modules.html It 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".
0

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.

3 Comments

I think what he's saying is that for some reason that appears to be not the case
@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"
Your answer covers only HTML 4. See hopeseekr's answer.
0

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.

7 Comments

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: The code needs to be parsed anyways. But your example will print \n literally.
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!
The fastest method is echo '<select name="',$varName,'_dd" id="',$varName,'e_dd">',"\n"; :)
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;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.