根据用户需求添加文字输入

发布于 2024-08-09 06:14:34 字数 719 浏览 2 评论 0 原文

<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" />
    }
}

?>

我想做的是要求用户在文本区域中输入一个值,然后我向他们提供适当数量的文本输入,供他们输入值。因此,用户输入 10,他们有提供 10 个文本输入和一个提交按钮或其他东西。我很欣赏这条线在原来的地方行不通,

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

但我确信这是正确的?另外,这条线有什么问题吗?

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

┊风居住的梦幻卍 2024-08-16 06:14:34

使用: 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" /><?
    }
}

?>

这将检查 $_GET 是否存在[ 'amountOFEntry'] (注意方括号,因为 $_GET 和 $_POST 是数组)

另请注意使用 ++$i 而不是 $i++。这里的性能略有提升。不多,但值得做。

编辑:::
请注意,变量将区分大小写,您在表单中使用 amountOfEntry 并在循环中使用 $_GET['amountOFEntry'] 。 (注意国会大厦F)

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)

迷爱 2024-08-16 06:14:34

$_GET 是一个数组,必须使用 [] 来获取元素。所以:

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

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

if(!empty($_GET['amountOFEntry']){
擦肩而过的背影 2024-08-16 06:14:34

正如所指出的,$_GET 返回一个值数组。因此,请使用方括号来查找所需的变量。而且你不能混合 HTML 和 PHP。因此,您需要将 HTML 设为字符串(通过引用它),然后用户 echo(或 print)来输出该字符串。

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

另外,正如 Lizard 所指出的,您应该使用 isset 来确定变量是否已设置。

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.

谎言月老 2024-08-16 06:14:34

您不妨获取 $_GET 的数值以避免运行时错误:

intval($_GET['amountOFEntry'])

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

intval($_GET['amountOFEntry'])
最初的梦 2024-08-16 06:14:34

如果您愿意,可以使用 JavaScript。使用像 JQuery 这样的库会有很大帮助。

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>

这需要大量工作,只是为了避免将页面发送回服务器并在服务器端执行工作,但我想我还是建议这样做......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文