Help- Using arrays for form...
Posted: Mon Aug 17, 2009 5:22 pm
Hello, i've been learning PHP and studying from a book i was working on an experimental form based on what i read in my book here's the code:
So when somebody checks those boxes this code will work:
And indeed it works but only if i check all 4 checkboxes, the book i have says
Code: Select all
<?php
echo "Hobbies<br/> <br/>";
echo "<form action=denemedeger.php form method='post'>";
echo "Music: <input type='checkbox' name='hobby[]' value='music'> ";
echo "Books: <input type='checkbox' name='hobby[]' value='books'> ";
echo "Movies: <input type='checkbox' name='hobby[]' value='movies'> ";
echo "Television: <input type='checkbox' name='hobby[]' value='television'> <br/>";
echo "<input type='submit' name='Submit' value='Enter'>";
echo "</form>";
?> Code: Select all
<?php
$hobbies = array(
$_REQUEST['hobby'][0],
$_REQUEST['hobby'][1],
$_REQUEST['hobby'][2],
$_REQUEST['hobby'][3]
);
foreach ($hobbies as $interests => $list) {
echo "$list";
}But if i uncheck any of those boxes i receive an error saying:Note that only the checked boxes will get passed to the PHP page.
What is my mistake? Did i misunderstood the book i have? I want it to be like, if somebody check "Music and Books" and submits he'll receive a message saying "Your hobbies are music and books" i want to achieve this by using arrays so i can learn the concept, thanks.Notice: Undefined offset: 3 in C:blablabla on line 15