...

View Full Version : printing an array from a form



geewhiz
04-28-2006, 05:10 AM
I have a form setup and I'm passing the values to a php file. I'm trying to trouble-shoot by printing the array that I'm getting from the form like so:

if($_GET){
$array = $_GET;
}else if($_POST){
$array = $_POST;
} else {
echo "You must Access this file through a form."; // If someone accesses the file directly, it wont work :)
}

echo "{$array['2']}";
echo "<br>";

Will this print the value in the key=2 of the array (of the 3rd value in array) created from the GET or POST functions? Because I can't get it to print any value from the array.

Nightfire
04-28-2006, 05:30 AM
Are the fields dynamic and you not know what the names are?

bustamelon
04-28-2006, 08:04 AM
Start simpler. Just:


if( isset( $_GET )) {
print_r( $_GET );
}

and/or:


if( isset( $_POST )) {
print_r( $_POST );
}

Another way:


if( isset( $_GET )) {
foreach( $_GET as $key=>$value ) {
echo "$key => $value<br>";
}
}

And what Nightfire is getting at is your form should have semantic field names, like for example, "username" or "password". In this case, you can reference those values by referring to them as $_POST["username"] or $_POST["password"] (or $_GET["username"] etc)

geewhiz
04-28-2006, 08:11 AM
the fields are dynamic. submitting form looks like so:


<input type="checkbox" name="answer[]" value="2"/>
input type="checkbox" name="answer[]" value="3"/>
<input type="radio" name="Topics" value="No"/>

geewhiz
04-28-2006, 08:17 AM
Thanks :thumbsup:

bustamelon
04-28-2006, 08:31 AM
the fields are dynamic. submitting form looks like so:

Right. Let's assume the method is POST. In this case:


if( isset( $_POST["answer"] )) {
foreach( $_POST["answer"] as $key=>$value ) {
echo "$key => $value<br>";
}
}

Or, since the $keys are just going to be numbered indices and you will know those already, just do:


if( isset( $_POST["answer"] )) {
foreach( $_POST["answer"] as $value ) {
echo "$value<br>";
}
}

geewhiz
04-28-2006, 08:20 PM
Will this correctly return the count of answer? Like so:


count($answer)

I'm using it in an loop like so:


for ($i=0; $i < count($answer); $i++) {

echo "The value of i is $i" . "<br>";

I get no values back for $i, never gets to the echo. It must be something wrong with the loop?

bustamelon
04-30-2006, 05:59 AM
Will this correctly return the count of answer?

only if you've assigned the posted form value to $answer like:

$answer = $_POST['answer'];

(or $_GET['answer'], whatever)

otherwise, use


for ($i=0; $i < count($_POST['answer']); $i++) {
echo "The value of i is $i" . "<br>";
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum