如何使用Blade模板和Laravel发送数据数组?

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

How to send array of data with blade templates and Laravel?

问题

I would like to send below section's data as an array to Laravel backend. Is there method to accomplish that without stringifying?

如何使用Blade模板和Laravel发送数据数组?

// preferred output

$members = [
    ["name" => "Sam", "location" => "US"],
    ["name" => "Mike", "location" => "UK"]
];
英文:

I would like to send below section's data as an array to Laravel backend. Is there method to accomplish that without stringifying?

如何使用Blade模板和Laravel发送数据数组?

// preferred output

$members = [
    ["name" => "Sam", "location" => "US"  ],
    ["name" => "Mike", "location" => "UK" ]
  ];

答案1

得分: 0

假设您有一个带有每行选择和位置的表单。

  • 首先循环并将所有值推送到一个数组中
  • 然后使用JSON.stringify()将数据转换为JSON格式

以下是示例代码片段:

var data = [];
document.querySelector("#submitForm").addEventListener("submit", (e) => {
   e.preventDefault();
    
   document.querySelectorAll(".member_name").forEach((member,index) => {
      let name = member.value;
      let location = member.nextElementSibling.value;
      data.push({name: name, location: location});
   });

   // 将数据转换为JSON格式
   data = JSON.stringify(data);
   console.log(data);
      
   // 将数据发送到后端
});
<form action="submit" method="post" id="submitForm">
  <div>
    <select name="member_name" class="member_name">
      <option value="member1" selected>Member 1</option>
      <option value="member2">Member 2</option>
      <option value="member3">Member 3</option>
    </select>
    <select name="member_location" class="member_location">
      <option value="location1" selected>Location 1</option>
      <option value="location2">Location 2</option>
      <option value="location3">Location 3</option>
    </select>
  </div>
  <div>
    <select name="member_name" class="member_name">
      <option value="member1">Member 1</option>
      <option value="member2">Member 2</option>
      <option value="member3">Member 3</option>
    </select>
    <select name="member_location" class="member_location">
      <option value="location1">Location 1</option>
      <option value="location2">Location 2</option>
      <option value="location3">Location 3</option>
    </select>
  </div>
  <div>
    <select name="member_name" class="member_name">
      <option value="member1">Member 1</option>
      <option value="member2">Member 2</option>
      <option value="member3">Member 3</option>
    </select>
    <select name="member_location" class="member_location">
      <option value="location1">Location 1</option>
      <option value="location2">Location 2</option>
      <option value="location3">Location 3</option>
    </select>
  </div>
  <input type="submit" value="Submit">
</form>
英文:

Assuming you have a form with select and location on each row.

  • First loop and push all values in one array
  • Then use JSON.stringfy() to convert the data in JSON format.

Here is the example snippet:-

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var data = [];
document.querySelector(&quot;#submitForm&quot;).addEventListener(&quot;submit&quot;, (e) =&gt; {
e.preventDefault();
document.querySelectorAll(&quot;.member_name&quot;).forEach((member,index) =&gt; {
let name = member.value;
let location = member.nextElementSibling.value;
data.push({name: name, location: location});
});
// Convert data into JSON format
data = JSON.stringify(data);
console.log(data);
// Send Data to backend
});

<!-- language: lang-html -->

&lt;form action=&quot;submit&quot; method=&quot;post&quot; id=&quot;submitForm&quot;&gt;
&lt;div&gt;
&lt;select name=&quot;member_name&quot; class=&quot;member_name&quot;&gt;
&lt;option value=&quot;member1&quot; selected&gt;Member 1&lt;/option&gt;
&lt;option value=&quot;member2&quot;&gt;Member 2&lt;/option&gt;
&lt;option value=&quot;member3&quot;&gt;Member 3&lt;/option&gt;
&lt;/select&gt;
&lt;select name=&quot;member_location&quot; class=&quot;member_location&quot;&gt;
&lt;option value=&quot;location1&quot; selected&gt;Location 1&lt;/option&gt;
&lt;option value=&quot;location2&quot;&gt;Location 2&lt;/option&gt;
&lt;option value=&quot;location3&quot;&gt;Location 3&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;select name=&quot;member_name&quot; class=&quot;member_name&quot;&gt;
&lt;option value=&quot;member1&quot;&gt;Member 1&lt;/option&gt;
&lt;option value=&quot;member2&quot;&gt;Member 2&lt;/option&gt;
&lt;option value=&quot;member3&quot;&gt;Member 3&lt;/option&gt;
&lt;/select&gt;
&lt;select name=&quot;member_location&quot; class=&quot;member_location&quot;&gt;
&lt;option value=&quot;location1&quot;&gt;Location 1&lt;/option&gt;
&lt;option value=&quot;location2&quot;&gt;Location 2&lt;/option&gt;
&lt;option value=&quot;location3&quot;&gt;Location 3&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;select name=&quot;member_name&quot; class=&quot;member_name&quot;&gt;
&lt;option value=&quot;member1&quot;&gt;Member 1&lt;/option&gt;
&lt;option value=&quot;member2&quot;&gt;Member 2&lt;/option&gt;
&lt;option value=&quot;member3&quot;&gt;Member 3&lt;/option&gt;
&lt;/select&gt;
&lt;select name=&quot;member_location&quot; class=&quot;member_location&quot;&gt;
&lt;option value=&quot;location1&quot;&gt;Location 1&lt;/option&gt;
&lt;option value=&quot;location2&quot;&gt;Location 2&lt;/option&gt;
&lt;option value=&quot;location3&quot;&gt;Location 3&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;

<!-- end snippet -->

You can adjust the html for errors and other stuff with or without blade syntax as you wish.

答案2

得分: 0

结构化您的HTML表单如下:

<select name="members[0][name]"></select>
<input type="text" name="members[0][location]">

等等...

members[index][name] 中的索引将是动态的,并逐渐增加。

在您的controller函数中,您将获得类似以下的members数组:

dd($request->members);
英文:

Structure your html form like below,

&lt;select name=&quot;members[0][name]&quot;&gt;
&lt;input type=&quot;text&quot; name=&quot;members[0][location]&quot;&gt;

and so on...

index in members[index][name] would be dynamic and increased by 1

And in your controller function you will get members array like,

dd($request-&gt;members);

huangapple
  • 本文由 发表于 2023年5月17日 20:53:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272346.html
匿名

发表评论

匿名网友

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

确定