tags:

views:

63

answers:

5

Revising for php and cant seem to get this to print the values out that i want

Any ideas?

Thanks

<form action="revision.php" method="GET">
<input type=“text” name=“number[]”/>
<input type=“text” name=“number[]”/>
<input type=“text” name=“number[]”/>
<input type=“text” name=“number[]”/>
<input type=“text” name=“number[]”/>
<input type="Submit" name="Calcuate"/>
</form>

<?php 
if(isset($_GET['number'])){
    $amount = count($number);

    for($i=0; $i < $amount; $i++){
        echo $number[$i];
    }
}
?>
A: 
 <form action="revision.php" method="GET" enctype="multipart/form-data">

Change form to this. The multipart tag must be used for this

you also need this for file uploads

and for printing use this

foreach ($_GET['number'] AS $key => $value)
{
    echo "$key => $value";
}

because the array can be number[1] -> number[3]

Marco
Does that let them be mapped to an array? If so, I wasn't aware of that!
Dolph
my lecture slides appear to say that we can create the form how i have done tho?
stan
Try it and print the get print_r($_GET); and you will see the data
Marco
+1  A: 

EDIT: My answer is completely wrong. See @rmarimon in comments below.

Text fields can't be mapped to an array. You'll have to name them something ugly like "number1", "number2", etc and add them up with $_GET['number1'] + ...

Dolph
Actually text fields can be mapped to arrays in the way @stan is doing. PHP maps all request variables ending in [] to an array and discards multiple values of variables not ending in []. You can check this here http://docs.php.net/manual/en/faq.html.php#faq.html.arrays
rmarimon
A: 

It is not in your code but do you have

$number = $_GET["number"]

What you are doing is the correct way. This is similar to this other question.

rmarimon
+2  A: 

I think the actual problem with your code is that the quotation marks " are wrong you are using “ and ” instead of ". Replace those and everything will work.

jondro
lol thanks very much, thats what you get when you copy sample code from powerpoint lecture slides :D
stan
A: 

The way I see it, there's a couple things you should change in your code, first, the names of the fields, you're trying to name them number[0], number[1], number[2] from the looks of it but it won't work that way, try naming them differenty or try to make a FOR cicle to create the fields with those custom names. Second, in order to save the array coming in the $_GET variable into the $number variable you need something like this:

if(isset($_GET['number']))
{
    $number = $_GET['number'];
    $amount = count($number);
    for( $i = 0 ; $i < $amount ; $i++ )
        echo $number[$i];
}

Hope this helps, if you're still having problems try posting or describing the context and what you have in mind for the form and the array.

Ozmah