首页 > 解决方案 > 如何将具有相同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 查询中

标签: javascriptphpjqueryajax

解决方案


身份证无所谓。您可以使用给定的名称传递多个值,

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

推荐阅读