简体   繁体   English

无法通过 PHP 中的表单 POST 方法接收数组

[英]Cant receive an array by form POST method in PHP

I'm having this problem where I just can't receive my data as an array我遇到了这个问题,我无法将我的数据作为数组接收

HTML: I'm sending it via Form inside a bootstrap modal like this HTML:我通过像这样的引导模式内的表单发送它

                 <div class="lines">
                    <div class="col-lg-4">
                      <input type="text" class="form-control"  
                      name="txtqtd[]" id="inputPassword2">
                    </div>
                  </div>

Thats my JS where I append multiple inputs with the same name这就是我的 JS,我在其中附加了多个具有相同名称的输入

                     $(function () {
                      $(".addDiv").click(function () {
                        novoDiv = $("div.lines:first").clone();
                        novoDiv.find("input").val("");
                        novoDiv.insertAfter("div.lines:last");
                        var esvaziaproduto = document.getElementsByName('txtqtd[]');
                      });
                    });

And thats how I get my data in php这就是我在 php 中获取数据的方式

$p_item = $_POST['txtqtd'];
$counter = count($p_item);

It just receives the first value of the array它只接收数组的第一个值

Does your JS actually send all the values and is it PHP that only receives the first one or does the JS only send 1 value?你的 JS 是否真的发送了所有的值,是 PHP 只接收第一个还是 JS 只发送 1 个值? (You can check in your console/network tab) (您可以检查您的控制台/网络选项卡)

If it's the first option:如果是第一个选项:

What kind of POST do you execute (JSON, form-encoded,..)?您执行哪种 POST(JSON、表单编码等)?

The incoming data for your PHP script could probably be like txtqtd=1,2,3,4 , so your $_POST['txtqtd'] = "1,2,3,4" , which results in a count of 1 .您的 PHP 脚本的传入数据可能类似于txtqtd=1,2,3,4 ,因此您的$_POST['txtqtd'] = "1,2,3,4"导致计数为1 Try var_dump() the result to make sure.尝试var_dump()结果以确保。

Then you'll probably need to explode the txtqtd value like $pieces = explode(',', $_POST['txtqtd]) which would then result into pieces -> (4)[1,2,3,4] with a count of 4 .然后你可能需要像$pieces = explode(',', $_POST['txtqtd]) txtqtd $pieces = explode(',', $_POST['txtqtd])这样的txtqtd$pieces = explode(',', $_POST['txtqtd])然后将结果变成pieces -> (4)[1,2,3,4]计数4 .

They are two solutions namely:它们是两种解决方案,即:

Solution 1. If posting by ajax解决方案 1. 如果通过 ajax 发布

$.ajax({ url: yoururl, type: $(this).attr("method"), //put method='post' in form dataType: "JSON", data: new FormData(this), //This will post every field in form as is processData: false, })

so at your server you will be able to access所以在您的服务器上您将能够访问

$_POST['txtqtd']; as array

Solution 2. If you want to first access the values from with in Javacript just use a simple method below解决方案 2. 如果您想首先访问 Javacript 中的值,只需使用下面的简单方法

var $p_item = $("input[name='txtqtd[]']")
          .map(function(){return $(this).val();}).get();
console.log($p_item);

Will give a JavaScript array将给出一个 JavaScript 数组

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

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