Explode在JS传递的字符串中无法正常工作。

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

Explode doesn't work with a string coming from JS

问题

<script>
    // 当按钮被点击时
    let tags = ''foo,bar'';
    elem.innerHTML += `<x-post tags="${tags}"></x-post>`
</script>
@php
    var_dump($tags); // string(9) "foo,bar"
    $tags = explode(',',$tags); // 分割字符串
    echo "<br>";
    var_dump($tags); // array(1) { [0] => string(9) "foo,bar"} 
@endphp
英文:

Javascript

<script>
    // when button clicked
    let tags = 'foo,bar';
    elem.innerHTML += `<x-post tags="${tags}"></x-post>`
</script>

post.blade.php

@php
    var_dump($tags); // string(9) "foo,bar"
    $tags = explode(',',$tags); // exploding
    echo "<br>";
    var_dump($tags); // array(1) { [0] => string(9) "foo,bar"} 
@endphp

How can I fix it? And why this is working correctly when I write tags="foo,bar"?

Expecting: array with foo and bar.

Tryed: JSON.stringify(), array, array with JSON.stringify().

Actually, I'm sending a post with tags via ajax so that they load after clicking on the button, like pagination. It works without tags, but such a problem has arisen with them.

Maybe you know how i can normally send an array to a blade component like in php?

Explode在JS传递的字符串中无法正常工作。

答案1

得分: -1

这可以通过创建一个额外的组件来解决。毕竟,问题在于我试图在php中处理字符串。

let html = '';
html += `<x-post title=${post.title}>${post.content}`;
tags.forEach(function(tag){
    html += `<x-tag>${tag.name}</x-tag>`;
})
html += `</x-post>`;
elem.innerHTML += html;
英文:

This can be solved by creating an additional component.
After all, the problem is that I tried to process the string in php

let html = &#39;&#39;;
html += `&lt;x-post title=${post.title}&gt;${post.content}`;
tags.forEach(function(tag){
    html += `&lt;x-tag&gt;${tag.name}&lt;/x-tag&gt;`;
})
html += `&lt;/x-post&gt;`;
elem.innerHTML += html;

答案2

得分: -2

你使用双引号将 tags 作为字符串文字发送,即 &lt;x-post tags=&quot;${tags}&quot;&gt;&lt;/x-post&gt;,因此返回的是 $tags 的总长度为9。

尝试不使用双引号,

&lt;script&gt;
// 在JS中
let tags = &#39;foo,bar&#39;;
elem.innerHTML += `&lt;x-post tags=${tags}&gt;&lt;/x-post&gt;`
&lt;/script&gt;

// post.blade.php
@php
    var_dump($tags); // string(7) &quot;foo,bar&quot;
    $tags = explode(&#39;,&#39;,$tags); // 分割
    echo &quot;&lt;br&gt;&quot;;
    var_dump($tags); // array(2) { [0]=&gt; string(3) &quot;foo&quot; [1]=&gt; string(3) &quot;bar&quot; } 
@endphp

希望这有所帮助 Explode在JS传递的字符串中无法正常工作。

英文:

You're sending tags in string literal with double quotes i.e. &lt;x-post tags=&quot;${tags}&quot;&gt;&lt;/x-post&gt; and therefore it's returning the total length of $tags 9.

Try without double quotes,

&lt;script&gt;
//in JS
let tags = &#39;foo,bar&#39;;
elem.innerHTML += `&lt;x-post tags=${tags}&gt;&lt;/x-post&gt;`
&lt;/script&gt;

// post.blade.php
@php
    var_dump($tags); // string(7) &quot;foo,bar&quot;
    $tags = explode(&#39;,&#39;,$tags); // exploding
    echo &quot;&lt;br&gt;&quot;;
    var_dump($tags); // array(2) { [0]=&gt; string(3) &quot;foo&quot; [1]=&gt; string(3) &quot;bar&quot; } 
@endphp

Hope it wll help Explode在JS传递的字符串中无法正常工作。

huangapple
  • 本文由 发表于 2023年8月5日 14:27:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840405.html
匿名

发表评论

匿名网友

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

确定