Question

I have the following page where a form is posted using ajax:

<form name="postdata">
    <input type="hidden" name="id" value="XXXX">
    <input type="hidden" name="action" value="add">
    <button type="submit" class="btn btn-success submit">Submit</button>
</form>

<script>
$(function()
{
    $('form[name="postdata"]').on('submit', function()
    {
        var data = $(this).serializeArray()
        $.post('api/Update.php', data, function(r,s)
        {
            console.log(r)
        });
    })
})
</script>

I have hundreds of forms like the one above on my page and It has became preferable to remove the redundand "action" parameter from the form action and add it to the JS function instead.

It should be something like this:

var data = $(this).serializeArray() + "&action=add"

That turns out as:

[object Object],[object Object]&action=add

Doesn't work but should be close, I'm just a total JS noob. Please help me.

Many thanks!

Was it helpful?

Solution

Thats because you are concatenating an object(an Array object) with a string, either use serialize method which returns a string or push an object into the array.

data.push({
    name: 'action',
    value: 'add'
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top