jQuery将键/值配对数组发送到PHP [英] Jquery sending key/value paired array to PHP

查看:100
本文介绍了jQuery将键/值配对数组发送到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说实话,我不确定如何提出这个问题,但是可以了.

To be honest Im not sure how to frame this question, but here goes.

我有一个包含以下输入字段的表单:

I have a form with the following input fields:

<input name="key[23]" type="text" />
<input name="key[29]" type="text" />
<input name="key[65]" type="text" />

我可以使用Jquery创建一个数组,将其发送到PHP脚本,如下所示:

I can create an array with Jquery to send to the PHP script like so:

    var new_keys = new Array();
$('input[name="keys[]"]').each(function() {
    new_keys.push($(this).attr('value'));
});

但是,当输入字段已经具有键时,我不知道如何创建数组: input name ="test [45]"

However I dont know how to create the array when the input fields already have a key: input name="test[45]"

我尝试将方法更改为使用类选择器而不是输入名称,但无法获取键名.

I tried changing my method to using a class selector instead of the input name, but I cant get the keyname.

我需要做的是将键名及其值发送给PHP,这样我就可以更新一个表,其中的键名等于表ID号.如果我不使用Ajax,这很容易做到.

What I need to do is send the keyname and its value to PHP so I can update a table where the keyname equals the table ID number. This is simple to do if I wasnt using Ajax.

希望如此.

已编辑

<form method="post" id="add-form" class="form" onsubmit="return false;">
<input class="required" type="text" name="name" id="name" />
<input class="required" type="text" name="keys[32]" />
<input class="required" type="text" name="keys[65]" />
<textarea name="Test"></textarea>
<input type="submit" name="submit" value="submit" />
</form>

$(document).ready(function() {
    $.fn.submitForm = function() {
        var name = $('#name').attr('value');
        var new_keys = new Array();
        $('input[name="keys[]"]').each(function() {
            new_keys.push($(this).attr('value'));
        });

        $.ajax({
            type: 'POST',
            data: { 'new_keywords' : new_keywords, 'name' : name },
            dataType: 'json',
            url: 'article/scripts/article.scripts.php',
            success: function($data) {
                var result = $data.result;
                var msg = $data.msg;

                if (result == "success") {
                    formMessageDisplay(msg, result);
                }
                else {
                    formMessageDisplay(msg, result);
                }
            },
            error: function($data) {
                formMessageDisplay('<?php echo AJAX_ERROR; ?>', result);
            }
        })
    }
});

这是我拥有的脚本的精简版.

This is a cutdown version of the script that I have.

推荐答案

您可以执行类似的操作,该操作应获取字段名称,然后在数组中分配值.然后,您可以根据需要在PHP中进行处理和清理:

You could do something like this which should grab the names of the fields and then assign the values in the array. You can then process and clean it up in PHP as needed:

var new_keys = new Array();
$('input[name="keys[]"]').each(function() {
    new_keys[$(this).attr('name')] = $(this).attr('value');
});
$.post('url/to/script/here.php', new_keys, function(data) {
    // process data here on success
}, 'json'); // expecting a json object

要进一步了解ajax,请参阅@this,以使用post方法 http://api.jquery. com/jQuery.post/

For further reading on ajax look @ this for using the post method http://api.jquery.com/jQuery.post/

这篇关于jQuery将键/值配对数组发送到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆