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

Nick Fitzsimons nick at nickfitz.co.uk
Wed Nov 29 08:56:51 CST 2006


On 29 Nov 2006, at 14:14:47, diego nunes wrote:

> On 11/29/06, John Deighan <jdeighan at pcgus.com> wrote:
>> For input elements of type "radio" and "checkbox", it is common to
>> define more than one related object, each of which have the same
>> 'name' property. In this case, data is submitted to the server with
>> this format:
>>
>>         name=value1,value2,...,valuen
>
>    As far as I know, in ASP you have the "bla=1,2,3,4" format, but it
> doesn't happen with all languages. I just don't get the point of why
> this happens, 'cause, in theory, it's the browser that handle this,
> but, anyway, try this:
> <form action="teste.asp">
>  <input type="text" name="teste" value="mimimi" />
>  <input type="text" name="teste" value="blabla" />
> </form>
>
>    When in ASP, it appear as "teste=mimimi,blabla", but in PHP it
> will be "teste=mimimi&teste=blabla", which gives you only one value
> when getting the querystring ($_GET['teste'] returns only "blabla").
> In PHP I always use name="DESIRED_NAME[]" (note the "[]" at the end),
> which gives you an Array with the values, in the server side language
> -- obviously it doens't work with ASP at all.
>

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"


and so on. 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.

As a general rule, the code on the client should never have to  
incorporate anything specific to the technology on the server, which  
is why it's better if form targets are URIs like "/something" rather  
than "/something.php" (although IIS makes this a bit tricky, having  
no useful equivalent of mod_rewrite).

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. If PHP can't cope with such a  
basic aspect of HTTP request URIs without client-side metacharacters,  
I think I may have to avoid it, despite having plans in the pipeline  
to consider it for several projects.

Regards,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/






More information about the Javascript mailing list