Page 1 of 1

How to print an array of $POST

Posted: Sun Oct 03, 2010 3:23 pm
by kelsjc
Hi all


This is probably the easiest question ever :), but I am not familiar with arrays , so hope somebody can help me


User post this checkbox array form:

<input type="checkbox" name="type[0][]" value="1" >
<input type="checkbox" name="type[0][]" value="2" >
<input type="checkbox" name="type[0][]" value="3" >

I need to get all checked values and print them ...
How can I do this?


I tried the code below, but it only returns me one value

Code: Select all


$values = array($_POST["type"]);

foreach ($values as $item){

     echo $item;

}


Any help???



Thanks!!

Re: How to print an array of $POST

Posted: Sun Oct 03, 2010 5:16 pm
by John Cartwright
With that form, you have a multi dimensional array. All your values are in the 2nd level deep, so to get those form values, you would do:

Code: Select all

foreach ($_POST['type'][0] as $item){
     echo $item;
}
I'm not exactly why you are using a multi dimensional array in this case, but thats out of the scope of your question.

Re: How to print an array of $POST

Posted: Sun Oct 03, 2010 5:48 pm
by kelsjc
John Cartwright wrote:With that form, you have a multi dimensional array. All your values are in the 2nd level deep, so to get those form values, you would do:

Code: Select all

foreach ($_POST['type'][0] as $item){
     echo $item;
}
I'm not exactly why you are using a multi dimensional array in this case, but thats out of the scope of your question.
Hey John, it worked! thanks!

one last thing: I have to use this information into my sql query:
I am trying something like this:

Code: Select all

foreach($_POST['type'][0] as $item){
     echo $item;
     $var1 = " OR gu.id_item='".$item."' ";
}

$var = " AND ($var1)";

$strSQL = "SELECT DISTINCT id FROM ".PROD_TABLE." gu WHERE gu.active='1' ".$var.";     
$rs = $dbconn->Execute($strSQL); 
I am getting error with the first "OR" in $var = AND (OR item1 OR item2 OR item3) -
How can I have $var = AND (item1 OR item2 OR item3) ?


Any idea?