How to submit a form as a JSON object

what I am doing is creating a form using JSON, this form can then be edited and create a new JSON object. The problem I am facing is getting the form id. The code I use to return a JSON object:

form = document.forms[0];
$.fn.serializeObject = function()
{
    alert("start serializeObject");
    var o = {};
    var a = this.seralizeArray();
    $.each(a, function(){
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
    alert(o);
};

$(function() {
    alert("here");
    form.submit(function(){
        result.append(JSON.stringify(form.serializeObject()));
        return false;
    });
});

It is just a refresh page. I'm not sure why. This program is not located on the server and is not used on the server. By this, I mean that only everyone will be running locally on the local machine without installing apache2.

Thank.

+5
source share
2 answers

You can write code easily. Here is how I do it:

Ajax:

$('#formID').on('submit',function () {
    $.ajax({
        url: 'submit.php',
        cache: false,
        type: 'POST',
        data : $('#formID').serialize(),
        success: function(json) {
            alert('all done');
        }
    });
});

Ajax, ? , PHP :

<?php
$json_object = json_decode($_POST);
?>
+9
$('#formID').on('submit',function (e) {
    e.preventDefault();
    $.ajax({
        url: 'submit.php',
        cache: false,
        type: 'POST',
        data : $('#formID').serialize(),
        success: function(json) {
        alert('all done');
    }
    });
});

, e.preventDefault();

+2

All Articles