【问题标题】:don't get input from field array POST不要从字段数组 POST 中获取输入
【发布时间】:2015-05-13 03:20:55
【问题描述】:

我从 google 下载了一个动态输入字段脚本,

但现在我想从中获取值。

这是脚本;

<script type="text/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['+x+']"/><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--;
    })
});
    </script>

HTML

<form action="addquestiontosection2.php" method="post">

<div class="input_fields_wrap">
            <button class="add_field_button">Add more values</button>
            <div><input type="text" name="mytext[1]"></input></div>
        </div>
</form>

PHP

$item1 = $_POST['mytext']['1'];

$item2= $_POST['mytext']['2'];

Item1 和 Item2 返回空。

非常感谢。

【问题讨论】:

  • 只有 1 个输入框分配了错误的索引
  • 它是动态的.. 所以如果你点击添加更多值.. 有两个字段。
  • @Elvira 你如何发布你的数据?你可以为“表单”标签放置整个 html
  • 您是否尝试在这些输入中写入一些文本?如果不是,它们是空的,这是正确的。
  • 是的,我已经尝试在这些输入中写入一些文本.. :)

标签: php forms dynamic field


【解决方案1】:

这就是你的html吗?如果那是一个表单,你应该把它放在一个表单中,而不是依赖按钮。

<div class="input_fields_wrap">
    <form method="post" action="somephpfile.php">
       <button class="add_field_button">Add more values</button>
       <div><input type="text" name="mytext[1]"></input></div>
    </form>
</div>

【讨论】:

  • 不要使用 name="mytext[1]",而是尝试使用 name="mytext1" 或在您的 php 文件中使用 $item1=$_POST['mytext'][1];
【解决方案2】:

您需要使用表单来发布数据;

<div class="input_fields_wrap">
  <form method="POST" action='#'>
     <button class="add_field_button">Add more values</button>
     <div><input type="text" name="mytext[1]"></input></div>
  </form>
</div>

通过使用“#”作为操作,表单会将数据发布到同一页面,因此您也必须在此页面上使用$item1 = $_POST['mytext']['1'];。如果您希望将其发布到外部 php 文件,您可以更改“操作”目标。

【讨论】:

    猜你喜欢
    • 2013-11-01
    • 2012-02-04
    • 2021-04-28
    • 2015-04-26
    • 2022-06-28
    • 1970-01-01
    • 2019-09-02
    • 2020-12-26
    相关资源
    最近更新 更多