Problem extracting array-indexed form data (using HTTP_POST_VARS) 
Author Message
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Hi,

I have a few text fields on a generated form which I want to name using
array convention, i.e. <input type=text name=text[symbolname]>.

In PHP I can extract the value the user supplied with: echo
$text["symbolname"];

That works like a charm.  But if I want to do it using the new-style
superglobal HTTP_POST_VARS, I can't get it working.

echo $HTTP_POST_VARS["text[symbolname]"]; doesnt give me any output.

Do you have an idea what I do wrong?

<?
if ($submit) {
        echo $text["gos.author"]; // works
        echo $HTTP_POST_VARS["text[gos.author]"]; // doesnt work
        }
else {
        echo "<form method=post>".
        "<input type=text name=text[gos.author]><br>".
        "<input type=text name=text[gos.age]><br>".
        "<input type=submit name=submit value=\" OK \">".
        "</form>";
        }
?>



Thu, 17 Mar 2005 04:17:23 GMT  
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Quote:

> <input type=text name=text[symbolname]>.

Attribute values must be quoted. Tags must be closed. Hence:
<input type="text" name="text[symbolname]" />

Quote:
> using the new-style superglobal HTTP_POST_VARS

This is the old non-superglobal array. The superglobal replacement is
called $_POST.

Quote:
> echo $HTTP_POST_VARS["text[symbolname]"]; doesnt give me any output.

Try
echo $_POST[ 'text' ][ 'symbolname' ];

--
Some people say that dying is hazardous for one's health.
Others say that nothing can travel faster than light.
I wonder what the logical connection between these statements is.



Thu, 17 Mar 2005 04:29:24 GMT  
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Quote:

> Try   echo $_POST[ 'text' ][ 'symbolname' ];

Thanks!

Works fine.  I can even loop through all posted values in a generic way.

<?
if ($submit) {

        if (is_array($_POST))
                foreach ($_POST as $postname => $postvalue) {
                        echo "_POST: $postname = $postvalue<br>";
                        if (is_array($postvalue)) {
                                echo "<blockquote>";
                                foreach ($postvalue as $name => $value)
                                        echo "$name = $value<br>";
                                echo "</blockquote>";
                                }
                        }

        }
else {
        echo "<form method=post>".
        //
        "<input type=hidden name=hidden[cfg.user.id] value=2187>".
        "<input type=hidden name=hidden[cfg.user.name] value=\"Blop
You\">".
        //
        "<input type=text name=text[gos.author]><br>".
        "<input type=text name=text[gos.age]><br>".
        "<input type=text name=text[scu.intake]><p>".
        //
        "<input type=radio name=radio[tar.sex] value=male checked>male".
        "<input type=radio name=radio[tar.sex] value=female>female<p>".
        //
        "<input type=radio name=radio[tar.age] value=young>young".
        "<input type=radio name=radio[tar.age] value=old
checked>old<p>".
        //
        "<select name=select[ice.flavour]>".
        "<option>vanilla".
        "<option>strawberry".
        "<option selected>rum and raisin".
        "<option>peach and orange".
        "</select><p>".
        //
        "<input type=checkbox value=1
name=checkbox[material.metal]>Metal ".
        "<input type=checkbox value=1 name=checkbox[material.plastic]
checked>Plastic ".
        "<input type=checkbox value=1
name=checkbox[material.fabric]>Fabric<p>".
        //
        "<input type=submit name=submit[submit.ok] value=\" Ok \"> ".
        "<input type=submit name=submit[submit.cancel] value=\" Cancel
\"> ".
        "</form>";
        }
?>

Sorry for the omission of the quotes.



Thu, 17 Mar 2005 06:22:21 GMT  
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Quote:
> <?

short tags are deprecated, you should use <?php

Quote:
> else {
>    echo "<form method=post>".

you should escape from php to output all this text...
also note that the , operator in echos is faster that the concatenation
. operator...

--
Flameeyes
http://drakeadmin.sf.net/ - First Stable Version Released

Please not toppost, use the right signature separation string and RTFM.



Thu, 17 Mar 2005 06:48:25 GMT  
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Quote:

> <?

While short tags are supposed to be still around for a long time, long
tags are generally better since they are always active (unlike short
tags which can be disabled in php.ini). Additionally, albeit imho only
important for people who do server-side XML processing of PHP documents
(a rare case), long tags are XML-compliant whereas short tags are not.
(And with short tags enabled, writing XML/XHTML documents requires
writing some messy code.)

Quote:
> if ($submit) {

Try using
if( isset( $_POST[ 'submit' ] ) ) {
here. It is longer but cleaner.

Quote:
>    if (is_array($_POST))

Isn't it always?

Quote:
>    echo "<form method=post>".
>    "<input type=hidden name=hidden[cfg.user.id] value=2187>". "<input
>    type=hidden name=hidden[cfg.user.name] value=\"Blop
> You\">".

[ snip some more lines like that ]

Opinions do differ at this point. Imnsho, it would be better to escape
from PHP here:
?>
<form method="POST" action="<?php echo $_SERVER[ 'PHP_SELF' ]; ?>">
        <input type="hidden" name="hidden[cfg.user.id]" value="2187" />
        <!-- Etc., the entire echo block. -->
<?php

This way is definitely more efficient than a concatenation-fest and for
many people also more readable. Another way, preferred by those who do
not work on HTML or do not like to separate it from PHP code is using
heredoc syntax.

Quote:
> Sorry for the omission of the quotes.

This is not something you apologize about, it's something you correct.
So, next time you write something, put quotes where quotes are due.

HTH

--
Some people say that dying is hazardous for one's health.
Others say that nothing can travel faster than light.
I wonder what the logical connection between these statements is.



Thu, 17 Mar 2005 06:49:08 GMT  
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Quote:

> > else {
> >       echo "<form method=post>".

> you should escape from php to output all this text...
> also note that the , operator in echos is faster that the concatenation
> . operator...

You were looking at a toy program to test a concept.  In production
software I never spit out HTML from script: I use templates to produce
pages.  I once worked at a telco and had to deal with large amounts of
script intermingled with ASP code.  Since that period I have an aversion
against mixing script with page text.  And, since I come from a C++
background I try to keep my PHP code as C-like as possible.  

Quote:

> > <?
> short tags are deprecated, you should use <?php

I didnt know that.  Is there a reason for this deprecation?


Thu, 17 Mar 2005 21:24:45 GMT  
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Quote:


> > if ($submit) {

> Try using
> if( isset( $_POST[ 'submit' ] ) ) {
> here. It is longer but cleaner.

Yes, I know that I now can use the superglobal _POST.

Quote:
> >       if (is_array($_POST))

> Isn't it always?

Indeed it is and I removed this statement from my test program.

Quote:
> >       echo "<form method=post>".

> Opinions do differ at this point. Imnsho, it would be better to escape
> from PHP here:
> This way is definitely more efficient than a concatenation-fest and for
> many people also more readable. Another way, preferred by those who do
> not work on HTML or do not like to separate it from PHP code is using
> heredoc syntax.

I like to separate it completely.  Normally I build my pages using a
template system:

        $t=new Template("templates");
        $t->set_file("src","categorieen.tpl");
        $t->set_var("header",implode("",file("templates/header.tpl")));
        $t->set_var("logo",implode("",file("templates/logo.tpl")));
        $t->set_var("footer",implode("",file("templates/footer.tpl")));
        $t->set_var("title","Hoofdpagina");
        $t->set_var("categorielist",$list);
        $t->pparse("output","src");

Quote:
> > Sorry for the omission of the quotes.

> This is not something you apologize about, it's something you correct.
> So, next time you write something, put quotes where quotes are due.

Opinion could differ on that, too.
I understand these quotes around values where they aren't needed
(because the values have no spaces) are a requirement of XHTML and/or
XML?  I like to omit them where possible because the resulting code
looks cleaner, and at the moment I only use plain old simple HTML.

Greetz,
--
Spam is spam no matter who's doing it or for what reason.



Thu, 17 Mar 2005 21:33:16 GMT  
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Quote:
> I understand these quotes around values where they aren't needed
> (because the values have no spaces) are a requirement of XHTML and/or
> XML?  I like to omit them where possible because the resulting code
> looks cleaner, and at the moment I only use plain old simple HTML.

not only for xhtml i guess..
however i think it's best to use the last standard of w3 on the net...

Quote:
> --

invalid signature delimiter, should be dash, dash, space.

--
Flameeyes
http://drakeadmin.sf.net/ - First Stable Version Released

Please not toppost, use the right signature separation string and RTFM.



Thu, 17 Mar 2005 21:53:50 GMT  
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Quote:



>>> if ($submit) {

>> if( isset( $_POST[ 'submit' ] ) ) {

> Yes, I know that I now can use the superglobal _POST.

This is not a "can" issue. Rather, a common PHP development premise is
that a script should not output errors/warnings/notices in any conditions.
While PHP is highly tolerant toward implicit declarations of variables, it
does actually not accept them as fully correct (and will output a notice
to that effect). Additionally, "sloppy" code often contains security
problems which are unnoticed in development. Since you are developing for
a hostile environment, always value security over brevity and make it a
habit (if only for the sake of consistent coding style).

Quote:
>>>    echo "<form method=post>".

>> it would be better to escape from PHP here:

> Normally I build my pages using a template system:

A good solution too, if your pages are more complicated than your template
system - mine usually aren't.

Quote:
>> > Sorry for the omission of the quotes.

>> So, next time you write something, put quotes where quotes are due.

> Opinion could differ on that, too.

No, this is a matter of language specifications.

Quote:
> I understand these quotes around values where they aren't needed
> (because the values have no spaces)

The quotes are needed because only you know whether the value has spaces
or not. The computer does not know whether you want only the next word as
the value or the entire chunk of text up to the EOF. The error tolerance
tends toward taking only the next word, but since we can avoid this
ambiguity altogether, why not do it?

Quote:
> are a requirement of XHTML and/or XML?

Yes.

Quote:
> I like to omit them where possible because the resulting code looks
> cleaner,

Your opinion. Again, the reader (human or not) might be confused about
where the value ends. Is there any real reason to make an exception for
values which do not contain the character 0x0020? I do not know any.

Quote:
> and at the moment I only use plain old simple HTML.

It is old - outdated - obsolete. Move with the times.

Quote:
> --

Incorrect signature delimiter, should be dash-dash-space.

--
Some people say that dying is hazardous for one's health.
Others say that nothing can travel faster than light.
I wonder what the logical connection between these statements is.



Thu, 17 Mar 2005 22:11:54 GMT  
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Quote:

> > --
> invalid signature delimiter, should be dash, dash, space.

I stand corrected, below.

--
Spam is spam no matter who's doing it or for what reason.



Thu, 17 Mar 2005 23:09:01 GMT  
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Quote:


> > Normally I build my pages using a template system:

> A good solution too, if your pages are more complicated than your template
> system - mine usually aren't.

I even use it for simple pages, for the sake of separating layout from
content.  The graphic designer can work on the templates and doesn't
have to dive into scripts.  Funny thing is that design is even further
separated because the templates use CSS style sheets.

Quote:
> [...about necessity of quotes...]  The error tolerance
> tends toward taking only the next word, but since we can avoid this
> ambiguity altogether, why not do it?

You convinced me.

Quote:
> It is old - outdated - obsolete. Move with the times.

That brings me to my next question.  I'm asked to do some code
conversion to PHP, what would be a good approach?  This is how the code
looks:

000010 IDENTIFICATION DIVISION.                            
000020 PROGRAM-ID. TABLE-PROG.                            
000030 AUTHOR.     TIMOTHY R P BROWN.
000048            
000050 ENVIRONMENT DIVISION.                                      
000060 INPUT-OUTPUT SECTION.                                      
000070 FILE-CONTROL.                                              
000080     SELECT TEAM-REC-IN ASSIGN TO "INPUT.REC"                
000090        ORGANIZATION IS SEQUENTIAL.                
000100     SELECT WORK-FILE ASSIGN TO SORTWK01.
000105* for MicroFocus compiler
000107* replace SORTWK01 with 'WORKFILE.DAT'
000110     SELECT SORT-OUT ASSIGN TO "SORTED.REC"                  
000120        ORGANIZATION IS SEQUENTIAL.                    
000130     SELECT PRINT-FILE ASSIGN TO PRINTER.            
000140                                                            
000150                                                            
000160 DATA DIVISION.                                              
000170 FILE SECTION.                                              
000180 FD TEAM-REC-IN.                                            
000190 01 TEAM-REC.                                                
000200     03  TEAM-CODE   PIC XXX.                                
000210     03  TEAM-NAME   PIC X(20).                              
000220     03  PLAYED      PIC 99.                          
000230     03  GOALS-FOR   PIC 99.                                
000240     03  GOALS-AGST  PIC 99.                                
000250     03  G-WON       PIC 99.                            
000260     03  G-LOST      PIC 99.                      
000270     03  G-DRAWN     PIC 99.                          
000280     03  GOAL-DIFF   PIC S99 SIGN LEADING SEPARATE.          
000290     03  POINTS      PIC 99.                          
000300                                                            
000310 SD WORK-FILE.                                              
000320 01 WORK-REC.                                                
000330     03  TEAM-CODE-KEY PIC XXX.                              
000340     03                PIC X(22).                            
000350     03  GF-KEY        PIC 99.                              
000360     03                PIC X(8).                            
000370     03  GD-KEY        PIC S99  SIGN LEADING SEPARATE.      
000380     03  POINTS-KEY    PIC 99.                              
000390                                                            
000400                                                            
000410 FD PRINT-FILE.                                              
000420 01 TEXT-OUT     PIC X(60).                                  
000430                                                            
000440 FD SORT-OUT.                                                
000450 01 TEAM-REC-OUT.                                            
000460     03  STEAM-CODE  PIC XXX.                                
000470     03  STEAM-NAME  PIC X(20).                              
000480     03  SPLAYED     PIC 99.                  
000490     03  SGOALS-FOR  PIC 99.                                
000500     03  SGOALS-AGST PIC 99.                                
000510     03  SG-WON      PIC 99.                  
000520     03  SG-LOST     PIC 99.                  
000530     03  SG-DRAWN    PIC 99.                                
000540     03  SGOAL-DIFF  PIC S999.                              
000550     03  SPOINTS     PIC 99.              
000560                                                            
000570                                                            
000580                                                            
000590                                                            
000600 WORKING-STORAGE SECTION.                                    
000610                                                            
000620 01 M              PIC 99.            
000630 01 REAL-GOAL-DIFF PIC S999.                                
000640                                                            
000650 01 W-DATE.                                                  
000660     03  W-YEAR    PIC 99.                      
000670     03  W-MON     PIC 99.                      
000680     03  W-DAY     PIC 99.                      
000690                                                            
000700                                                            
000710 01 SCORE.                                                  
000720     03 W-H-SCR    PIC 9.                      
000730     03            PIC X VALUE "-".            
000740     03 W-A-SCR    PIC 9.                        
000750                                                            
000760 01 P-TITLE.                                                
000770     03            PIC X(5) VALUE SPACES.        
000780     03 TAB-TITLE  PIC X(34)                              
000790         VALUE "The English FA Premier League".              
000800                                                            
000810     03 P-DATE.                                              
000820         05 P-DAY  PIC XX.                                
000830         05        PIC X VALUE "/".              
000840         05 P-MON  PIC XX.                                
000850         05        PIC X VALUE "/".                
000860         05 P-YEAR PIC XX.                                
000870                                                      
000880 01 P-UNDERLINE    PIC X(45) VALUE ALL "-".            
000890 01 P-GAP          PIC X VALUE SPACE.                      
000900                                                            
000910 01 P-HEADER.                                                
000920     03          PIC X(6) VALUE SPACES.        
000930     03 TAB-TEAM PIC X(4) VALUE "TEAM".                      
000940     03          PIC X(11) VALUE SPACES.        
000950     03 PLY      PIC X(5) VALUE "Playd".                    
000960     03          PIC X VALUE SPACE.                
000970     03 WO       PIC XXX VALUE "Won".                        
000980     03          PIC X VALUE SPACE.              
000990     03 DR       PIC XXXX VALUE "Drwn".                      
001000     03          PIC X VALUE SPACE.              
001010     03 LO       PIC XXXX VALUE "Lost".                      
001020     03          PIC X VALUE SPACE.            
001030     03 GF       PIC XXX VALUE "For".                        
001040     03          PIC X VALUE SPACE.                
001050     03 GA       PIC X(5) VALUE "Agnst".                    
001060     03          PIC X VALUE SPACE.                
001070     03 GD       PIC XX VALUE "GD".                          
001080     03          PIC X VALUE SPACE.              
001090     03 PTS      PIC XXX VALUE "PTS".                        
001100                                                            
001110 01 W-TEXT-OUT.                                              
001120     03  P-TAB-POS   PIC 99.                                
001130     03              PIC X VALUE SPACE.            
001140     03  P-TEAM      PIC X(20).            
001150     03  P-PLAYED    PIC 99.                                
001160     03              PIC XXX VALUE SPACES.        
001170     03  P-G-WON     PIC Z9.                      
001180     03              PIC XX VALUE SPACES.          
001190     03  P-G-DRAWN   PIC Z9.                                
001200     03              PIC XXX VALUE SPACES.    
001210     03  P-G-LOST    PIC Z9.                                
001220     03              PIC XXX VALUE SPACES.    
001230     03  P-GOALS-FOR PIC 99.                                
001240     03              PIC XX VALUE SPACES.        
001250     03  P-GOALS-AGST  PIC 99.                              
001260     03              PIC XX VALUE SPACES.        
001270     03  P-GOAL-DIFF PIC ZZ9.                                
001280     03              PIC XX VALUE SPACES.      
001290     03  P-POINTS    PIC Z9.                        a        
001300                                                            
001310                                                            
001320 01 SCORE-TAB.                                              
001330     03 TAB-SCORE    PIC 9 OCCURS 2.                        
001340 01 T-POINTS-TAB.                                            
001350     03 T-POINTS     PIC 99 OCCURS 20.            
001360 01 POINTS-TAB.                                              
001370     03  TAB-POINTS  PIC 9 OCCURS 2.                        
001380 01 T-G-FOR-TAB.                                            
001390     03 T-G-FOR      PIC 99 OCCURS 20.          
001400 01 T-G-AGST-TAB.                                            
001410     03 T-G-AGST     PIC 99 OCCURS 20.        
001420 01 T-G-DIFF-TAB.                                            
001430     03
...

read more »



Thu, 17 Mar 2005 23:21:38 GMT  
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Quote:

> I'm asked to do some code conversion to PHP, what would be a good
> approach?

Judging from what you posted, automatical conversion is plausible.
However, if this is the only program you have to convert, doing it
manually will be faster.

Quote:
> This is how the code looks:

Sorry, me not speak COBOL (which is what I think it is).

--
Some people say that dying is hazardous for one's health.
Others say that nothing can travel faster than light.
I wonder what the logical connection between these statements is.



Thu, 17 Mar 2005 23:51:14 GMT  
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Quote:
> > > <?
> > short tags are deprecated, you should use <?php

> I didnt know that.  Is there a reason for this deprecation?

Not 100% certain, but the XML DTD is
<?xml version="1.0"?>

http://www.w3.org/TR/REC-xml#sec-prolog-dtd

This would create errors on php pages which expected

<?

as the start of a section of php code.

regards,
reggie



Sat, 19 Mar 2005 06:35:51 GMT  
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Quote:

> Not 100% certain, but the XML DTD is
> <?xml version="1.0"?>

Afaik there is no XML DTD. DTDs are used to specify the structure of
_XML-based_ languages. Btw, the short tags vs. XML problem can be solved
by echoing the tag from PHP. (Messy but should work.) Or switching
short_tags off for the entire file (.htaccess).

--
Some people say that dying is hazardous for one's health.
Others say that nothing can travel faster than light.
I wonder what the logical connection between these statements is.



Sat, 19 Mar 2005 07:10:51 GMT  
 Problem extracting array-indexed form data (using HTTP_POST_VARS)

Quote:

> > Not 100% certain, but the XML DTD is
> > <?xml version="1.0"?>

> Afaik there is no XML DTD. DTDs are used to specify the structure of
> _XML-based_ languages.

A quick reglance at the link I originally posted shows my error was calling
it the DTD when it is the XML decleration.

regards,
reggie.



Sat, 19 Mar 2005 08:58:28 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. extracting top 100 data using awk or sed/perl

2. how to index the data onto specific places in the array

3. Help needed indexing data from a scaled array...

4. Indexing Data from a cluster to an array

5. Extracting columns from Array withSubstitution problem

6. Extract data from radius's raw data

7. Extract an array from an array of clusters

8. Extracting data from COBOL data files

9. Using output of one array as an index to another

10. using viable data form an Excel

11. Using an index in an array

12. Undefined offsets on posted form using an array

 

 
Powered by phpBB® Forum Software