5 Replies - 239 Views - Last Post: 21 May 2013 - 03:27 AM Rate Topic: -----

#1 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Array not printing

Posted 19 May 2013 - 04:26 PM

Ok so on i have a basic html form one of the field of this form is called "name="pkg[1]" it has to be called that other wise my jquery will not work..

So i do a
 print_r($_POST);


to see what the page which the form is submit to is getting and im getting

Array ( [advert_type] => 1 [pkg] => Array ( [1] => 99 [2] => 99 ) 


i need to print out just pkg[1] and pkg[2]
so i do a simple echo like so
echo $pkg['1'];


but shows nothing. Can anyone help me ??

Is This A Good Question/Topic? 0
  • +

Replies To: Array not printing

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,373
  • Joined: 27-December 08

Re: Array not printing

Posted 19 May 2013 - 04:32 PM

Remember that pkg is a post value. So it is stored in the $_POST superglobal. You have to access it through there.
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: Array not printing

Posted 19 May 2013 - 04:33 PM

You need to extract it from the $_POST array first:

$pkg = $_POST['pkg'];
echo $pkg['1'];    // or $pkg[1]

Was This Post Helpful? 1
  • +
  • -

#4 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: Array not printing

Posted 19 May 2013 - 04:37 PM

Thanks all. I did do
echo $_POST['pkg']; 

but showed nothing.
But after i used
$pkg = $_POST['pkg'];
echo $pkg['1'];    // or $pkg[1]

all worked fine. Thanks
Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: Array not printing

Posted 20 May 2013 - 07:24 PM

View Postandrewsw, on 19 May 2013 - 11:33 PM, said:

You need to extract it from the $_POST array first:

$pkg = $_POST['pkg'];
echo $pkg['1'];    // or $pkg[1]

Not really.
There is no difference between doing that, and this:
echo $_POST['pkg']['1'];



Copying user input into global variables - without filtering and/or sanitation - is a bad habit to get into. Leave unfiltered user input where it belongs: in the request super-globals.
Was This Post Helpful? 1
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: Array not printing

Posted 21 May 2013 - 03:27 AM

echo $_POST['pkg']['1'];

Yes, sorry, it is not necessary to extract the array from $_POST, particularly if you are only reading one or two values.

Doing $pkg = $_POST['pkg']; is a slight convenience. However, I generally prefer to keep the distinction between (raw/unsanitised) $_POST data clear, and only copy this to other variables or arrays once I've checked, and sanitised, it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1