繁体   English   中英

生成多个表单输入 php

[英]Generate multiple Form Inputs php

提交时如何获取值我正在根据用户选择通过循环生成输入,但不知道如何通过 post 方法检索输入值

这是我所拥有的样本

// string is based on database values it can be anything which i can't tell
Example code

$string = 'math,english,biology';

$exp = explode(',', $string);
foreach($exp as $value){
    print '<input type="text" name="'.$value.'[]" value=""  />
}

将值放入value="" ,命名字段并将其设为数组[]

$string = 'math,english,biology';

$exp = explode(',', $string);
foreach ($exp as $value) {
    echo '<input type="text" name="fieldName[]" value="<?= htmlentities($value) ?>"  />
}

然后它将在 * $_POST['fieldName']中作为数组访问。

*假设您在表单上使用method="POST"

如果math,english,biology是表单键,则执行以下操作:

$string = 'math,english,biology';

$exp = explode(',', $string);
foreach ($exp as $key) {
    echo '<input type="text" name="fieldName[<?= htmlentities($key) ?>]" value=""/>
}

或者

$string = 'math,english,biology';

$exp = explode(',', $string);
foreach ($exp as $key) {
    echo '<input type="text" name="<?= htmlentities($key) ?>" value=""/>
}

您不必使用名称数组( name="blabla[]"

$string = 'math,english,biology';

$exp = explode(',', $string);


if ($_POST) {
    foreach ($exp as $name) {
        if (isset($_POST[$name])) {
            echo 'input ' . $name . ' is ' . $_POST[$name] . '<br>';
        }
    }
    exit();
}


echo '<form method="post">';
foreach($exp as $value){
    print '<input type="text" name="'.$value.'" value=""  />';
}
echo '<button type="submit">Submit</button></form>';

在每个输入中输入 a、b、c 并提交。 结果如下:

输入数学是
输入英文是b
输入生物学是 c

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM