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

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

Push the value to input array field

问题

以下是翻译好的部分:

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

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

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

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

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

&lt;input type=&quot;hidden&quot; class=&quot;deleted-images&quot; name=&quot;deletedImages[]&quot; value=&quot;&quot;&gt;
const deleteButtons = document.querySelectorAll(&#39;.delete-resource&#39;);
const removedImagesInput = document.querySelector(&#39;.deleted-images&#39;);

deleteButtons.forEach((item) =&gt; {
    item.addEventListener(&#39;click&#39;, function () {
       item.closest(&#39;.input-photo-preview&#39;).style.display = &#39;none&#39;;
       removedImagesInput.value += parseInt(item.getAttribute(&#39;data-image-id&#39;)) + &#39;,&#39;;

    });
})

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

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

答案1

得分: 2

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

const deleteButtons = document.querySelectorAll('.delete-resource');
const removedImagesInput = document.querySelector('.deleted-images');
const removedImagesInputParent = removedImagesInput.parentNode;
// 防止提交空值
removedImagesInputParent.removeChild(removedImagesInput);

deleteButtons.forEach((item) => {
    item.addEventListener('click', function () {
       item.closest('.input-photo-preview').style.display = 'none';
       // 创建新的<input>元素
       const elInput = removedImagesInput.cloneNode(false);
       elInput.value = item.getAttribute('data-image-id');
       // 将新的<input>插入DOM中
       removedImagesInputParent.appendChild(elInput);
    });
})

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

$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:

const deleteButtons = document.querySelectorAll(&#39;.delete-resource&#39;);
const removedImagesInput = document.querySelector(&#39;.deleted-images&#39;);
const removedImagesInputParent = removedImagesInput.parentNode;
//prevent submitting empty value
removedImagesInputParent.removeChild(removedImagesInput);

deleteButtons.forEach((item) =&gt; {
    item.addEventListener(&#39;click&#39;, function () {
       item.closest(&#39;.input-photo-preview&#39;).style.display = &#39;none&#39;;
       //create new &lt;input&gt; element
       const elInput = removedImagesInput.cloneNode(false);
       elInput.value = item.getAttribute(&#39;data-image-id&#39;);
       //insert new &lt;input&gt; into DOM
       removedImagesInputParent.appendChild(elInput);
    });
})

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:

$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:

确定