I have a form which I wanna send via AJAX so I'll use serialize jQuery function.

This is the code (not all the elements has "name", but anyway the alert should display something):

http://jsfiddle.net/T6Rz3/2/


JS

$('#add-producto').click(function (e) {

    alert($('#datos-add').serialize())

    $.ajax({
        type: "POST",
        url: "insertar.php",
        data: $('#datos-add').serialize(),
        success: function (data) {
            alert(data);
        }
    });


    e.preventDefault();

});

HTML

<form id="#datos-add">
    <div class="form-horizontal">
        <div class="modal modal-block">
            <div class="modal-header">
                    <h3>Añadir productos</h3>

            </div>
            <div class="modal-body">
                <div class="control-group">
                    <label class="control-label">Marca</label>
                    <div class="controls">
                        <input type="text" name="marca" id="marca" class="input-xlarge">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Producto</label>
                    <div class="controls">
                        <input type="text" name="descr" id="descr" class="input-xlarge">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Tallajes</label>
                    <div class="controls controls-row">
                        <input id="tallajes" value="España">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Referencias</label>
                    <div class="controls controls-row">
                        <input id="referencias" class="input-xlarge">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Número de tallas</label>
                    <div class="controls controls-row">
                        <input type="number" min="1" max="99" value="5" id="numero-tallas" class="span1">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">Primera talla</label>
                    <div class="controls controls-row">
                        <input type="text" id="primera-talla" value="XS" class="span1">
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <div class="btn-group">
                    <button id="borrar" class="btn" type="button">Borrar todo</button>
                    <button id="generar" class="btn btn-primary" type="button">Generar tabla</button>
                </div>
            </div>
        </div>
        <div class="form-horizontal">
            <div id="formulario-medidas" class="modal modal-block">
                <div class="modal-header">
                        <h4>Tabla de medidas</h4>

                </div>
                <div class="modal-body">
                    <table id="tabla-medidas" class="table table-bordered"></table>
                </div>
                <div class="modal-footer">
                    <button id="medidas-completadas" class="btn btn-success" type="button">Medidas completadas</button>
                </div>
            </div>
            <div id="formulario-tallajes" class="modal modal-block">
                <div class="modal-header">
                        <h4>Tabla de tallajes</h4>

                </div>
                <div class="modal-body">
                    <table id="tabla-tallajes" class="table table-bordered"></table>
                </div>
                <div class="modal-footer">
                    <button id="add-producto" class="btn btn-block btn-large btn-danger" type="button">Añadir producto</button>
                </div>
            </div>
        </div>
    </div>
</form>

Do you see what I'm doing wrong?

Any tip, advide or help will be appreciated, and if you need more info, let me know and I'll edit the post.

有帮助吗?

解决方案

Your form id is "datos-añadir" but your JavaScript refers to "datos-add".

Here is a jsfiddle fork that works. All I did was fix the reference:

$('#add-producto').click(function (e) {

    var $form = $('#datos-añadir');
    alert($form.serialize());
    return false;

其他提示

Fist of all, there is a typo. It should be <form id="datos-add"> instead of <form id="#datos-add">.

But I also think you are serializing it the wrong way.

When you do a POST ajax call, you shoud use .serializeArray(), instead of .serialize().

The .serialize() will join all key/values like a query string. The .serializeArray() will include all key/values in the request.

In short, try:

$('#add-producto').click(function (e) {
    $.ajax({
        type: "POST",
        url: "insertar.php",
        data: $('#datos-add').serializeArray(),
        success: function (data) {
            alert(data);
        }
    });
    e.preventDefault();
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top