Submitting form information to an array

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
russellcurtis
Forum Newbie
Posts: 2
Joined: Thu Dec 16, 2004 4:47 am

Submitting form information to an array

Post by russellcurtis »

patrikG | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Sorry if this is a stupid question, but I'm really struggling to get arrays
working in PHP for form submissions.

I've found a few tutorials which have the following form / submission pages:

// form.html

Code: Select all

<form action="submit.php" method=POST>
Which pets do you have?<br>
<input type="checkbox" name=petsї] value="dog">Dog
<input type="checkbox" name=petsї] value="cat">Cat
<input type="checkbox" name=petsї] value="gorilla">Gorilla
<input type="submit" name="languages" value="Submit"><br>
</form>
// submit.php

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', TRUE);

print "You have the following pets ";
$number=count($pets);
for($a=0;$a<=$number;$a++){
echo $pets[$a];
}
However, this page generates errors:

You have the following pets
Notice: Undefined variable: pets in
/var/www/html/marketing/drawings/submit.php on line 7

Notice: Undefined variable: pets in
/var/www/html/marketing/drawings/submit.php on line 9


WHY!?!

Thanks in advance.

Russell
patrikG | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

First.

To retreive variables from a POST from a forum use $_POST

Code: Select all

$number=count($_POST&#1111;"pets"]);
To print the content you could use foreach

Code: Select all

<?php
foreach ($_POST["pets"] as $value) {
   echo $value.'<br/>';
}
?>
and always you can dump de variables to show the contents like (put it after de ini_set)

Code: Select all

<?php
echo '<pre>';
print_t($_POST);
echo '</pre>';
?>
auth
Forum Newbie
Posts: 1
Joined: Thu Dec 16, 2004 5:30 am
Location: php-gaming.org

Post by auth »

Hello,

You haven't defined the pets varible that is $pets. What your code is basically doing is calling on two undefined varibles. Maybe do something like this:

if (! $_POST["pets"]) {
$pets = 0;
}

Before the print command. I don't think you can make varibles like $pets[]...

//The second post was posted when this one was written so i was unaware of the post.
russellcurtis
Forum Newbie
Posts: 2
Joined: Thu Dec 16, 2004 4:47 am

Good work

Post by russellcurtis »

Thanks, chaps. This works fine.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php
 if (! $_POST["pets"]) { 
$pets = 0; 
} 

?>
would require for register globals to be on, which is off by default.

try:

Code: Select all

<?php
if (!empty($_POST["pets"])) { 
$pets = 0; 
} 
?>
Post Reply