0

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];
    }
}
?>
stan
  • 433
  • 1
  • 5
  • 7

5 Answers5

2

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
  • 619
  • 3
  • 9
  • lol thanks very much, thats what you get when you copy sample code from powerpoint lecture slides :D – stan Apr 23 '10 at 14:28
1

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
  • 49,714
  • 13
  • 63
  • 88
  • 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 – Ricardo Marimon Apr 23 '10 at 14:08
0
 <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
  • 2,306
  • 2
  • 26
  • 43
0

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.

Community
  • 1
  • 1
Ricardo Marimon
  • 10,339
  • 9
  • 52
  • 59
0

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
  • 140
  • 8