将该值推送到输入数组字段。

huangapple go评论97阅读模式
英文:

Push the value to input array field

问题

以下是翻译好的部分:

如何向具有名称为数组的输入添加元素?

  1. <input type="hidden" class="deleted-images" name="deletedImages[]" value="">

在 PHP 输出中,我希望得到一个数组:

  1. deletedImages => [1,2,...n]
英文:

How can I add elements to input with name as array?

  1. &lt;input type=&quot;hidden&quot; class=&quot;deleted-images&quot; name=&quot;deletedImages[]&quot; value=&quot;&quot;&gt;
  1. const deleteButtons = document.querySelectorAll(&#39;.delete-resource&#39;);
  2. const removedImagesInput = document.querySelector(&#39;.deleted-images&#39;);
  3. deleteButtons.forEach((item) =&gt; {
  4. item.addEventListener(&#39;click&#39;, function () {
  5. item.closest(&#39;.input-photo-preview&#39;).style.display = &#39;none&#39;;
  6. removedImagesInput.value += parseInt(item.getAttribute(&#39;data-image-id&#39;)) + &#39;,&#39;;
  7. });
  8. })

At the output in PHP I want to get an array:

  1. deletedImages =&gt; [1,2,...n]

答案1

得分: 2

为了让PHP接收一个数组,您需要具有多个元素,它们的name属性应为"deletedImages[]"
例如,您可以为每个新值克隆您现有的.deletedImages元素:

  1. const deleteButtons = document.querySelectorAll('.delete-resource');
  2. const removedImagesInput = document.querySelector('.deleted-images');
  3. const removedImagesInputParent = removedImagesInput.parentNode;
  4. // 防止提交空值
  5. removedImagesInputParent.removeChild(removedImagesInput);
  6. deleteButtons.forEach((item) => {
  7. item.addEventListener('click', function () {
  8. item.closest('.input-photo-preview').style.display = 'none';
  9. // 创建新的<input>元素
  10. const elInput = removedImagesInput.cloneNode(false);
  11. elInput.value = item.getAttribute('data-image-id');
  12. // 将新的<input>插入DOM中
  13. removedImagesInputParent.appendChild(elInput);
  14. });
  15. })

否则,您需要发送JSON或逗号分隔的文本(就像您的尝试一样),然后在PHP中解码它成为一个数组,类似于:

  1. $deletedImages = explode(",", $_POST['deletedImages']);
英文:

In order for PHP to receive an array, you'd need multiple elements with name=&quot;deletedImages[]&quot;.
For example, you can clone your existing .deletedImages element for each new value:

  1. const deleteButtons = document.querySelectorAll(&#39;.delete-resource&#39;);
  2. const removedImagesInput = document.querySelector(&#39;.deleted-images&#39;);
  3. const removedImagesInputParent = removedImagesInput.parentNode;
  4. //prevent submitting empty value
  5. removedImagesInputParent.removeChild(removedImagesInput);
  6. deleteButtons.forEach((item) =&gt; {
  7. item.addEventListener(&#39;click&#39;, function () {
  8. item.closest(&#39;.input-photo-preview&#39;).style.display = &#39;none&#39;;
  9. //create new &lt;input&gt; element
  10. const elInput = removedImagesInput.cloneNode(false);
  11. elInput.value = item.getAttribute(&#39;data-image-id&#39;);
  12. //insert new &lt;input&gt; into DOM
  13. removedImagesInputParent.appendChild(elInput);
  14. });
  15. })

Otherwise, you'd need send JSON or comma separated text (as your attempt does) and in php decode it into an array with something like:

  1. $deletedImages = explode(&quot;,&quot;, $_POST[&#39;deletedImages&#39;]);

huangapple
  • 本文由 发表于 2023年5月13日 21:22:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定