Page 1 of 1

Help- Using arrays for form...

Posted: Mon Aug 17, 2009 5:22 pm
by rozkan
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:

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>"; 
?> 
So when somebody checks those boxes this code will work:

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";
 
}
And indeed it works but only if i check all 4 checkboxes, the book i have says
Note that only the checked boxes will get passed to the PHP page.
But if i uncheck any of those boxes i receive an error saying:
Notice: Undefined offset: 3 in C:blablabla on line 15
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.

Re: Help- Using arrays for form...

Posted: Mon Aug 17, 2009 6:23 pm
by requinix
If you only check two boxes then only [0] and [1] will be set. There is no [2] or [3] so if you try to access them PHP will complain.

$_REQUEST["hobby"] is already an array. You don't need $hobbies.

Code: Select all

foreach ($_REQUEST["hobby"] as $number => $interest) {
    echo $interest, "<br>\n";
}

Re: Help- Using arrays for form...

Posted: Mon Aug 17, 2009 6:27 pm
by jackpf
That's a stupid way of doing it, no offense.

Why put the values into an array when they're already in an array?

This would be better (and should get rid of the errors):

Code: Select all

foreach($_REQUEST['hobby'] as $list)
echo "$list";



Oh damn you tasairis :P

Re: Help- Using arrays for form...

Posted: Mon Aug 17, 2009 6:38 pm
by rozkan
Thanks for help both.

Re: Help- Using arrays for form...

Posted: Tue Aug 18, 2009 2:11 pm
by McInfo
@jackpf

Edit: This post was recovered from search engine cache.

Re: Help- Using arrays for form...

Posted: Tue Aug 18, 2009 2:19 pm
by jackpf
Are you referring to my initial comment or my obscenities directed towards tasairis (which was a joke btw)?

My initial comment was not to imply that the OP himself is stupid, rather, the method the book teaches is stupid, because it's not the most efficient method, and is in fact, doing something that's already been done.

If I have offended you in any way, you have my profuse apologies...

Re: Help- Using arrays for form...

Posted: Tue Aug 18, 2009 3:16 pm
by McInfo
jackpf wrote:Are you referring to my initial comment or my obscenities directed towards tasairis (which was a joke btw)?
I was referring to the combined effect of both.
jackpf wrote:If I have offended you in any way, you have my profuse apologies...
You've redeemed yourself.

Edit: This post was recovered from search engine cache.

Re: Help- Using arrays for form...

Posted: Tue Aug 18, 2009 3:17 pm
by jackpf
I did not mean either of them in an offensive way...

I guess I was too hasty in presuming others can handle a joke.