[Javascript] error in "JavaScript: The Definitive Guide"?

diego nunes dnunes at gmail.com
Wed Nov 29 09:20:42 CST 2006


On 11/29/06, Nick Fitzsimons <nick at nickfitz.co.uk> wrote:
> ASP automatically creates a collection into which you can index. So
> you would use (verbose JScript example - it can be shorter):
>
> Request.QueryString("teste").Item(1) // returns the value "minimi"
> Request.QueryString("teste").Item(2) // returns the value "blabla"
>
> (blablabla)
>
> The raw data that travels over the wire is in the format
>
> teste=minimi&teste=blabla
>
> which modern server-side technologies should parse into some useful
> data structure which you then access.

    I'd never actually checked the URI that browser create when
posting a form to an ASP or PHP file. It makes a lot more sense that
it is the server-side technology is handling the
"name=value&name=value2" then the browser (creating something like
"name=value,value2" for one kind of language and
"name=value&name=value2" for another -- in fact, the browser would
never know which technology you're using, because you can even have
PHP Scripts running under .asp extension).

> If PHP actually requires you to include the [] in the
> HTML, as in
> <input type="checkbox" name="teste[]" value="minimi">
> then I would say it's considerably more limiting: for example, this
> would require (potentially extensive) modification of existing pages
> if I move from another server-side solution to PHP. I don't have
> enough experience with PHP to know if those brackets are in fact
> required to get it to handle multiple name-value pairs with the same
> name, but I do know that ASP and JSP manage to cope with such
> requests without requiring metacharacters to be inserted into client-
> side markup.
    PHP maps your fields names to variable, and it have a way of doing
"$varname[]='bla'" to add an element to an Array ("$bla[]='value1';
$bla[]='value2';" would result in an Array with two indexes, 0 and 1,
containing "value1" and "value2" as its values, respectively). That's
why the square brackets works well when handling multiple values --
the language itself don't make any effort more than eval('$'.
INPUT_NAME .'="'. INPUT_VALUE .'"'); (note that PHP uses dot as
concatenation operator) for both cases: simple variables, and arrays.
The language core doesn't do any effort to handle multiple querystring
variables with the same name -- it leaves it up to you, programmer.
    I agree that it's such a weak way of dealing, and of course it
would be a pain to migrate from one language to another (I'm also an
adopter of never showing which technology is being used in a website
or application, by using only "/name" instead of "/name.ext"), but I
think the language, PHP, when compared with other options with the
same printfoot and power, is a worthwhile choice.

    Anyway, _backing to the cold meal_, it seems that, if the book
doesn't cover all the cases, especially the case of such a popular
language, it could be changed in some way to indicate that the mileage
can vary, and that the considered behavior is not a constant.


    What do you guys think?

--
diego nunes
dnunes.com



More information about the Javascript mailing list