【问题标题】:HTML form submit multiple valuesHTML表单提交多个值
【发布时间】:2014-11-27 03:22:45
【问题描述】:

我需要将提交的水果名称打印在“Sadrzaj korpe:”之后,即“苹果、橙子、香蕉”。

我的代码只打印一个水果。

<form>  
    <label for="voce">Voce:</label>
    <input type="text" name="voce">
    <input type="submit" name="submit" value="Ubaci voce u korpu"><br>

 </form>  
    <?php
 if ($_GET["submit"]){
        if ($_GET["voce"]){ 
                echo "Sadrzaj korpe je ".$_GET['voce'];
            }
        }           

【问题讨论】:

  • 您必须将这些值存储在某个地方(例如在数据库中),然后在提交时将它们全部打印出来。
  • 您写了if -&gt; get submit -&gt; get voce -&gt; print out value of voce ... 并没有暗示将您的价值保存在数据库或数组中。如果你想保存它,使用 then 一个数组来保存它。
  • 您想在一个 GET 请求中提交多个水果吗?
  • @dukaPA 你有几个选择,你想要一个可以提交的固定数量的水果,还是一个动态的数量?
  • 动态@AndrewBreksa

标签: php forms


【解决方案1】:

关于后端(来自here):

input type="text" name="hoursWorked[]" /&gt; 将在内部转换为$_POST['hoursWorked'] 下的数组。

这意味着您可以使用表单执行以下操作:

<form method="post" action="collect_vals.php">
<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div><input type="text" name="mytext[]"></div>
    <div><input type="text" name="mytext[]"></div>
    <div><input type="text" name="mytext[]"></div>
    <div><input type="text" name="mytext[]"></div>
    <div><input type="text" name="mytext[]"></div>
</div>
</form>

然后,在 PHP 中:

<?php
if(isset($_POST["mytext"])){       
    $capture_field_vals ="";
    foreach($_POST["mytext"] as $key => $text_field){
        $capture_field_vals .= $text_field .", "; //Here is where the values are concated to $capture_field_vals
    }
    echo $capture_field_vals;
}
?>

关于动态形式(来自here):

JavaScript:

$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID
    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

HTML 表单本身:

<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div><input type="text" name="mytext[]"></div>
</div>

【讨论】:

    【解决方案2】:

    首先,您可以使用isset() 检查$_GET、$_POST 等值。

    其次,如果我没听错的话,你想一个接一个地提交几个水果并实时显示?

    为此,您应该使用Ajax。

    否则,如果您的目标只是动态显示添加到客户端的数据,那么请使用带有 AngularJS 之类的库或任何其他使用双向数据绑定的库的客户端。

    如果只是向用户显示数据更改数据集,您不需要服务器端吗?当用户完成添加他的水果列表时,您可以将数据发送到服务器。

    【讨论】:

      【解决方案3】:

      参考这个link

        if (isset($_POST["submit"])) {
              echo "Yes";     
        }else{  
             echo "N0";
        }
      

      【讨论】:

      • 这似乎是一个完全不同的问题的答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-05
      相关资源
      最近更新 更多