我在这样的文件中有多个输入:
<form action="card_generate.php">
<input type="text" name="tZero">
<input type="text" name="tOne">
<input type="text" name="tTwo">
<input type="text" name="tThree">
</form>
$tabs = array($_POST["tZero"], $_POST["tOne"], $_POST["tTwo"], $_POST["tThree"]);
有没有办法可以通过循环或其他方法将这些值放在数组中,而不是逐个将每个值放在一个数组中,可以有多于4个值的值。
答案 0 :(得分:2)
使用输入名称数组
<form action="card_generate.php">
<input type="text" name="t[]">
<input type="text" name="t[]">
<input type="text" name="t[]">
<input type="text" name="t[]">
</form>
而且,你会得到帖子,
print_r($_POST['t']);
答案 1 :(得分:1)
您还可以使用示例中显示的foreach
循环:
<强> HTML 强>
<form method="POST">
<input type="text" name="tZero">
<input type="text" name="tOne">
<input type="text" name="tTwo">
<input type="text" name="tThree">
<input type="submit" name="">
</form>
<强> PHP 强>
<?php
if ( isset($_POST) ) {
foreach ($_POST as $key => $value) {
echo "Name: $key, value: $value";
echo "<br>";
}
}
?>
<强> RESULT 强>
Name: tZero, value: first
Name: tOne, value: hi
Name: tTwo, value: firthfds
Name: tThree, value: fourth value
P.S。不要忘记method="POST"
中的<form>
。
答案 2 :(得分:0)
您是否有理由说RuntimeError:
can't modify frozen Array
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/actionpack-5.1.0/lib/action_dispatch/middleware/stack.rb:74:in `insert'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/actionpack-5.1.0/lib/action_dispatch/middleware/stack.rb:74:in `insert'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/actionpack-5.1.0/lib/action_dispatch/middleware/stack.rb:81:in `insert_after'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/request_store-1.3.2/lib/request_store/railtie.rb:5:in `block in <class:Railtie>'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:30:in `instance_exec'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:30:in `run'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:59:in `block in run_initializers'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:58:in `run_initializers'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/application.rb:353:in `initialize!'
# ./config/environment.rb:5:in `<top (required)>'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `require'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `block in require'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `require'
# ./spec/rails_helper.rb:12:in `<top (required)>'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `require'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `block in require'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `require'
# ./spec/controllers/admin/capacity_charges_controller_spec.rb:3:in `<top (required)>'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `load'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `block in load'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1@Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `load'
?
答案 3 :(得分:-1)
您可以使用array_push:http://php.net/manual/de/function.array-push.php
你可以这样循环:
for($i = 0; $i < 10; $i++) {
if(isset($_POST["t" . $i])) {
array_push($array, $_POST["t" . $i]);
}
}