文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>mootools将表单变成json格式(一)

mootools将表单变成json格式(一)

时间:2007-08-12  来源:linxh

本文转自:http://forum.mootools.net/viewtopic.php?id=4765   原文如下:   if you are trying to send htmls elements arrays to json, you'll get problem. json.toQuery does not work with forms, and form.toQueryString is disigned to work with ajax, not json.

so i spend some time to extend elements class to this :

Element.extend({
    toJsonString:function(){
        var formElements = {};
        var j=0;
        this.getFormElements().each(function(el){
            var name = el.name;
            var value = el.getValue();
            if (value === false || !name || el.disabled) return;
            if (formElements[name.replace('[]', "")])
                formElements[name.replace('[]', "")].push(value);
            else formElements[name.replace('[]', "")] = [value];
        });
 
        for (var element in formElements){
            if (formElements[element].length == 1)
                formElements[element] = formElements[element][0];
        }
        return Json.toString(formElements);
    }
});

at least with php, this function do send vars and arrays through json to php. all you have to do is

var jSonRequest = new Ajax('my_json_data.php', {
                    data: $('myform').toJsonString(),
                    onComplete: my_oncomplete
                });
jSonRequest.request();

and you'll have your form ready to be send.

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载