Page 1 of 1

Submitting form information to an array

Posted: Thu Dec 16, 2004 4:48 am
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]

Posted: Thu Dec 16, 2004 5:31 am
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>';
?>

Posted: Thu Dec 16, 2004 5:37 am
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.

Good work

Posted: Thu Dec 16, 2004 5:50 am
by russellcurtis
Thanks, chaps. This works fine.

Posted: Thu Dec 16, 2004 6:53 am
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; 
} 
?>