开发者

Add text inputs based on user's requirement

<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'm 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 inputs for them to enter their values in. So the user enters 10, they have 10 text inputs p开发者_StackOverflow社区resented and a submit button or something. I appreciate this line won't 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


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)


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

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


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.


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

intval($_GET['amountOFEntry'])


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜