How to print an array of $POST

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kelsjc
Forum Newbie
Posts: 2
Joined: Sun Oct 03, 2010 3:20 pm

How to print an array of $POST

Post 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!!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How to print an array of $POST

Post 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.
kelsjc
Forum Newbie
Posts: 2
Joined: Sun Oct 03, 2010 3:20 pm

Re: How to print an array of $POST

Post 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?
Post Reply