Forum Moderators: coopster

Message Too Old, No Replies

php checkboxes

         

ayushchd

7:29 pm on Jul 30, 2007 (gmt 0)

10+ Year Member



Can anyone tell me how to store the selected checkbox values in a variable or an array..

d40sithui

7:47 pm on Jul 30, 2007 (gmt 0)

10+ Year Member



im actually working with this so i'll lend you a hand. see below for a sample. notice the "[]" in the checkbox name. you need this if you want to retrieve it as an array later.

/////HTML FORM
<form name="myform" action="action.php" method="post">
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" name="list[]" value="java">Java<br>
<input type="checkbox" name="list[]" value="javascript">Javascript<br>
<input type="checkbox" name="list[]" value="php">PHP<br>
<input type="checkbox" name="item1" value="ajax">AJAX<br>
<input type="checkbox" name="item2" value="css">CSS<br>
<input type="submit" value="Submit">
</form>

//////ACTION.PHP
$list = POST['list']; //assigns to array
$item1 = POST['item1']; //assigns to var
$item2 = POST['item2']; //assigns to another var

echo $list[0]; //prints "java"
echo $list[1]; //prints "javascript"
echo $list[2]; //prints "php"
echo $item1; //prints "ajax"
echo $item2; //prints "css"

coopster

1:05 am on Aug 8, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The PHP manual pages on PHP and HTML [php.net] are also a great resource.