Help- Using arrays for form...

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
rozkan
Forum Newbie
Posts: 10
Joined: Thu Jul 02, 2009 8:08 pm

Help- Using arrays for form...

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help- Using arrays for form...

Post 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";
}
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Help- Using arrays for form...

Post 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
rozkan
Forum Newbie
Posts: 10
Joined: Thu Jul 02, 2009 8:08 pm

Re: Help- Using arrays for form...

Post by rozkan »

Thanks for help both.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Help- Using arrays for form...

Post by McInfo »

@jackpf

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 6:48 pm, edited 1 time in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Help- Using arrays for form...

Post 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...
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Help- Using arrays for form...

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 6:48 pm, edited 1 time in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Help- Using arrays for form...

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