【问题标题】:Sending data combined in a form as an array in AJAX/JQuery在 AJAX/JQuery 中以数组形式发送数据组合
【发布时间】:2016-03-08 10:12:32
【问题描述】:

我需要一些帮助来发送我的数据。

首先,我工作的脚本被用作 Nagios 的 Mass Acknoledgments 工具,其灵感来自 NagiosXI 的可购买组件。我最后开发了它,数据处理/发送完全在 PHP 中。实际上,我更新了我的脚本,为此,我需要使用 AJAX。

我遇到的问题是这样的:下面是我的表单的屏幕,生成它的 html 代码,Chrome 调试工具的屏幕,向您展示我的数据是如何发送的,以及用于数据检索的 PHP 代码。

Mass Acknoledgments Tools

    <tr>
   <td class="OK">168VWL1</td>
   <td><a href="javascript:checkAll('host1');">Check all for this hosts</a></td>
   <td class="empty"></td>
   <td class="empty"></td>
   <td class="empty"></td>
   <td class="empty"></td>
</tr>
<tr>
   <td class="empty"></td>
   <td class="critical"><input class="host1 servicecheck" type="checkbox" name="services[]" value="168VWL1::Explorer">Explorer</td>
   <td class="output">Explorer.exe: not running</td>
   <td class="centerd"><input type="checkbox" class="sticky" name="sticky[]" value="168VWL1::Explorer"></td>
   <td class="centerd"><input type="checkbox" class="notify" name="notify[]" value="168VWL1::Explorer"></td>
   <td class="centerd"><input type="checkbox" class="persist" name="persist[]" value="168VWL1::Explorer"></td>
</tr>

Data sending

if(isset($_POST['hosts'])){
        $allHosts = json_decode($_POST['hosts']);}

    if(isset($_POST['services'])){
        $allServices = json_decode($_POST['services']);}

    if(isset($_POST['sticky'])){
        $allStickys = json_decode($_POST['sticky']);}

    if(isset($_POST['notify'])){
        $allNotifys = json_decode($_POST['notify']);}

    if(isset($_POST['persistent'])){
        $allPersistents = json_decode($_POST['persistent']);}

如您所见,通过这种方式,在 PHP 中处理我的数据非常简单,只需一个调用,我的数组就已经生成了。

现在,因为我使用 AJAX,所以我不知道如何以同样的方式发送这个。有什么建议吗?

【问题讨论】:

    标签: php jquery html ajax nagios


    【解决方案1】:

    序列化并发布您的表单数据 -

    var formData = $('.campaign-form').serialize();
    $.ajax({
        type: 'post',
        url: POST_URL_HERE,
        data: formData,
        success: function(data)
        {
            console.log(data);
        }
    });
    

    【讨论】:

      【解决方案2】:

      $('#form1').serialize()

      使用 ajax 发送表单数据

      【讨论】:

      • 好方法,但例如,我的数据现在是这样的:“services%5B%5D=168VWL1%3A%3AExplorer&services%5B%5D=ESXI1%3A%3AChecking+the+ESXI+硬件”是否可以将其转换为 JSON ?
      猜你喜欢
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多