p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Quick question


Message #1 by "Gerry Vandermaesen" <gerry@a...> on Wed, 5 Feb 2003 18:46:00 +0100
Hi all,

I have a HTML form which posts certain variables as an array, but have
the following problem:

echo($_POST['other']['service']) returns nothing, the variable seems
empty.

However, when I do:

foreach ($_POST['other'] as $key => $val) {
	echo($key . ': ' . $val);
}

the output is:

'service': test

What am I overlooking?

Regards,

Gerry.

Message #2 by "Pedro Graca" <bzzzt@f...> on Wed, 05 Feb 2003 18:15:39 +0000
On Wed, 5 Feb 2003 18:46:00 +0100, "Gerry Vandermaesen"
<gerry@a...> said:
> Hi all,
[...]
> echo($_POST['other']['service']) returns nothing
[...] 
> foreach ($_POST['other'] as $key => $val) {
> 	echo($key . ': ' . $val);
> }
> 
> the output is:
> 
> 'service': test
__^_______^__

You have single quotes in the array name!
try
  echo($_POST['other']['\'service\'']);


you might want to change error_reporting to E_ALL in php.ini
or call
  error_reporting(E_ALL);
for the scripts you're debugging/developing

with E_ALL calling
  echo($_POST['other']['service']);
outputs 
  Notice: Undefined index: service in ....

with E_ALL & ~ E_NOTICE (the default) there's no indication of what is
wrong


Happy coding :-)
-- 
                                            ()    ribbon campaign     ()
                                            /\   against HTML mail    /\
Message #3 by "Nikolai Devereaux" <yomama@u...> on Wed, 5 Feb 2003 12:59:37 -0800
> > the output is:
> >
> > 'service': test
> __^_______^__
>
> You have single quotes in the array name!
> try
>   echo($_POST['other']['\'service\'']);

Pedro's right, but it should be mentioned that there's really no reason to
submit a quoted field (name or value) unless you really need the quotes in
there.

nik

Message #4 by "Gerry Vandermaesen" <gerry@a...> on Thu, 6 Feb 2003 08:44:37 +0100
Thanks Pedro and Nikolai for your replies. If I get you right, I should
just name my input field "other[service]" instead? I guess I got a bit
confused with the PHP notation of arrays then.

Greetings,

Gerry.

> > the output is:
> >
> > 'service': test
> __^_______^__
>
> You have single quotes in the array name!
> try
>   echo($_POST['other']['\'service\'']);

Pedro's right, but it should be mentioned that there's really no reason
to submit a quoted field (name or value) unless you really need the
quotes in there.

nik

Message #5 by "Pedro Graca" <bzzzt@f...> on Thu, 06 Feb 2003 11:07:35 +0000
On Thu, 6 Feb 2003 08:44:37 +0100, Gerry Vandermaesen said:
> Thanks Pedro and Nikolai for your replies. If I get you right, I should
> just name my input field "other[service]" instead? I guess I got a bit
> confused with the PHP notation of arrays then.

yes, that's right
  <input type="text" name="other[service]" />

will be reflected in PHP as
  $_POST['other']['service']



you can even do this funny (obfuscated) thing:
  option 1: <input type="text" name="opt[]" value="one" />
  option 2: <input type="text" name="opt[]" value="two" />
  option 3: <input type="text" name="opt[]" value="three" />
                    ____________________^^__
PHP will number sequentially the opt array indexes

in your PHP you can do:
  if ($_POST['opt'][1] == 'two') {
    echo 'Please change the second option.'
  }


aren't arrays a wonderful thing?
-- 
                                            ()    ribbon campaign     ()
                                            /\   against HTML mail    /\
Message #6 by "Nikolai Devereaux" <yomama@u...> on Thu, 6 Feb 2003 16:36:42 -0800
To add another comment, it's not really PHP's notation of arrays, it's how PHP
parses HTML form input.

You can't nest arrays within indexes in HTML forms; that is, you can't do
this:

  <input type="text" name="other[services[1]]" />

But you can create nested arrays:

  <input type="text" name="other[services][automotive]" />

Read my tip on using arrays in forms to organize input data for more examples:

  http://p2p.wrox.com/archive/beginning_php/2002-08/52.asp


Take care,

Nik


  Return to Index