tags:

views:

78

answers:

6

Hello all,

I have this:

foreach ($country as $value){

The contents of $country using print_r is this:

Array

However, when I print_r all of POST variables it shows me country as single array rather than an array within an array as the above suggests?

Array ( 
    [entry] => http://www.google.com
    [SelectLocation] => Canada
    [country] => Array ( 
        [0] => AL
        [1] => AS 
    )
    [type] => country
    [destination] => http://yahoo.co.uk
    [default] => http://yahoo.com
    [loginsubmit] =>
)

What's going on?

Thanks all

Update

I have done this:

$country = sanitize($_POST['country']);

So the contents of $_POST['country'] is $country, right?!

+1  A: 

Are you sure you don't need $_POST['country'] instead of just plain $country?

ceejayoz
+1  A: 

Ok .. lets get this straight

You have a $_POST Array like this?

Array ( 
   [entry] => http://www.google.com
   [SelectLocation] => Canada 
   [country] => Array ( 
                        [0] => AL 
                        [1] => AS 
               ) 
   [type] => country 
   [destination] => http://yahoo.co.uk 
   [default] => http://yahoo.com 
   [loginsubmit] => 
)

Which you are trying to send through a foreach loop:

foreach($_POST['country'] as $value)
{
    echo $value . "<br>";
}

and should output

AL
AS

Is that right?

Chacha102
+1  A: 

Where is $country coming from? Are you assuming that $_POST variables are auto-extracted into the current scope? Because they are not. Perhaps you want this instead?

foreach ( $_POST['country'] as $value) {
Peter Bailey
+1  A: 

I don't see any problem here:

$country = $_POST['country'];
foreach ($country as $value){
    echo $value . '<br />';
}

If you're confused because the $_POST array contains an array for the country key, the explanation is square brackets after the HTML name attribute, which ensures that PHP gets the collection as an array:

<select name="country[]" multiple="multiple">
...
</select>

or perhaps:

<input type="checkbox" name="country[]" value="US"/>
<input type="checkbox" name="country[]" value="UK"/>
karim79
+4  A: 

Sounds like you were expecting register_globals to be on (which would create your $country variable from the POST) but that setting is not on.

So, your options are:

  1. Use $_POST['country'] instead of $country (good idea).
  2. Turn on register_globals (bad idea!)
Eric Petroelje
A: 

Ok, my sanitize function was terrible. I have sovled the problme. That function seemed to discard arrays since it only checks strings! Sorry everyone. :)

Abs