我如何在提交時獲取值我正在根據用戶選擇通過回圈生成輸入,但不知道如何通過 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="" />
}
uj5u.com熱心網友回復:
您不必使用名稱陣列 ( 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 并提交。結果如下:
輸入數學是a
輸入英語是b
輸入生物是c
uj5u.com熱心網友回復:
將值放入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=""/>
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364916.html
