javascript - 如何将具有相同ID和名称的多个文本框值传递给ajax查询字符串
问题描述
<input type="text" name="b_destinations[]" id="title"/>
<input type="text" name="b_destinations[]" id="title"/>
<input type="text" name="b_destinations[]" id="title"/>
<input type="text" name="b_destinations[]" id="title"/>
以上是我的代码,您可以看到我有多个具有相同名称和 ID 的文本框。现在我使用没有ajax函数的php进行提交。使用 foreach 和 json_encode 作为旅行预订计划,我将这些值保存在单个数据库列中。下面是我正在使用的查询的 php 代码。
foreach($_POST['b_destinations'] as $p_destination) {
$pdata[] = mysqli_real_escape_string($mysqli,$p_destination);
}
$pData[] = $pdata;
$b_destinations = json_encode($pData);
我在没有 ajax 的情况下保存了这个表单,但现在我将使用 ajax 来提交表单。我如何将这个具有相同名称和 ID 的多个文本框值传递给 ajax 数据字符串,然后将其发布到我的 php 查询中
解决方案
身份证无所谓。您可以使用给定的名称传递多个值,
var b_destinations = document.getElementsByName('b_destinations[]');
var b_destinationsArr= [];
for(var i=0;i<b_destinations.length;i++)
b_destinationsArr[] = b_destinations[i].value;
// Ajax call with b_destinationsArr array in POST action
推荐阅读
- binary - Artillery.io - POST 二进制数据
- elasticsearch - 弹性搜索 - 按时差排除记录
- ios - 如何为两个不同的pickerviews(swift4)调用2个不同的数组
- java - 如何在请求开始时创建 Context 对象的条目并在请求结束时检索它?
- javascript - React.js - 单击按钮时移动组件
- javascript - Codeigniter - 编码、解码 JSON
- textures - 虚幻引擎 4 在导入时扭曲了一些纹理
- azure - Azure 应用程序网关 DNS
- javascript - ES6 休息参数代码不起作用
- ios - 如何在 Swift 中将 Speed 设置为“moveTo”方法?