tags:

views:

74

answers:

5
<form action = "numbericalInput.php" method = "Get">

Please enter the number of input areas you wish 
<input type = "text" name = "amountOfEntry"/>
<input type = "submit" name = "GO"/>

</form>

<?php

if(!empty($_GET("amountOFEntry")){
    for($i = 0; $i < $_GET("amountOFEntry"); $i++){
        <input type= "text" name = "nums[]" size = "2" />
    }
}

?>

What i am trying to do is ask the user to input a value in to the text area and then for me to present them with an appropriate amount of text areas for them to enter their values in. So the user enters 10, they have 10 text boxes presented and a submit button or something. I appreciate this line wont work where it is

<input type= "text" name = "nums[]" size = "2" />

but I am sure that's along the right sort of lines? also, what is wrong with this line?

if(!empty($_GET("amountOFEntry")){

thanks

+2  A: 

use: isset() http://php.net/manual/en/function.isset.php

<form action = "numbericalInput.php" method = "Get">

Please enter the number of input areas you wish 
<input type = "text" name = "amountOfEntry"/>
<input type = "submit" name = "GO"/>

</form>

<?php

if(isset($_GET['amountOfEntry'])){
    for($i = 0; $i < $_GET['amountOfEntry']; ++$i){
        ?><input type= "text" name = "nums[]" size = "2" /><?
    }
}

?>

This will check for the existence of $_GET['amountOFEntry'] (Note square brackets as $_GET and $_POST are arrays)

Please also note use of ++$i instead of $i++. There is a minor performance increase here. Not much but it worth doing.

EDIT::: Please note that the variables will be case sensitive, You are using amountOfEntry in the form and $_GET['amountOFEntry'] in the loop. (Note capitol F)

Lizard
`$_GET("amountOFEntry")` is invalid syntax, it should be `$_GET["amountOFEntry"]`
Ben James
sorry only changed 1 of them.. updated both now
Lizard
Please take note of my latest comment regarding the variable name case
Lizard
This is a good solution, but I would check to see if $_GET['amountOfEntry'] is numeric (http://php.net/is_numeric()), because that is the only valid input for the for loop.
iddqd
+1  A: 

$_GET is an array, you have to use [] to get the elements. So:

if(!empty($_GET['amountOFEntry']){
Ikke
A: 

As pointed out $_GET returns an array of values. So use the square brackets to find the variable you want. Also you cant mix HTML amd PHP. So you need to make the HTML a string (by quoting it) and user echo (or print) to output the string.

if(!empty($_GET["amountOFEntry"]){
    for ($i = 0; $i < $_GET["amountOFEntry"]; $i++) {
        echo '<input type= "text" name = "nums[]" size = "2" />';
    }
}

Also, as noted by Lizard, you should use isset to determine if the variable is set.

dotty
i still cant manage to print the correct number if text boxes using this approach tho :S
Ricki Lambert
Ricki, please recheck my answer I have added someone which I think will fix it, regarding the case of the variable.
Lizard
A: 

You might as well get the numerical value of the $_GET to avoid runtime errors:

intval($_GET['amountOFEntry'])
jerjer
A: 

If you preferred you could use JavaScript. Using a library like JQuery would help a lot.

JavaScript:

$("#goButton").bind("click",function(e){
  numberOfEntries = parseInt($("#numberOfEntries").attr("value"));
  for(i=0;i<numberOfEntries;i++){
   newInput = document.createElement("input");
   $(newInput).attr("type","text").attr("name","nums[]").attr("size","2");
   $("#inputEntries").append(newInput);
  }
 }
);

HTML:

<body>
<input id="numberOfEntries" type = "text" name = "amountOfEntry"/>
<input id="goButton" type = "submit" name = "GO"/>
<div id="inputEntries"></div>
</body>

This is a lot of work just to avoid sending the page back to the server and having the work carried out on the server-side, but thought I might suggest it anyway...

Rew