简体   繁体   中英

jQuery $.post doesn't send data

I'm trying to send data via $.post but when I'm check the network section of Firefox, it seems that data is never sent, yet the success event is triggered. I don't know what's wrong... I tried with $.post and $.ajax and I got the same result.

HTML

<button id="nuevoadmin">Agregar administrador</button>

      <article id="addadmin">

            <form id="newadmin">

                <label>Nombre</label> <input type="text" name="nomadmin" required/>
                <label>Puesto</label> 
                <select name="puesto" id="jobs">

                </select>
                <label>Correo electrónico</label> <input type="email" name="emailadmin" required/>
                <span class="vacio">Campo requerido</span>
                <span class="malcorreo">Introduzca una dirección de correo válida</span>
                <label>Clave de acceso</label> <input type="password" name="claveadmin" required/>
                <span class="badpass">La contraseña sólo puede contener de 6 a 10 caracteres alfanuméricos</span>

                <button id="enviaadmin">Agregar</button>

            </form>

        </article>

jQuery for ajax functions

$(document).ready(function() {

    muestraemp();
    $('#nuevoadmin').on('click', listaPuesto);
    $('#admins table tbody').on('tr td:last-child', 'click', function() {

        var rowIndex = $(this).closest('tr').data('value');
        modifica(rowIndex);

    });

    $('#lightbox').ready(function() {

        $('#enviaadmin').on('click', function() {

            var datos = $(this).parent('form').serialize();
            enviar(datos);

        });

    });

});

function listaPuesto() {

    var puestos = '';

    var uno = '<option value="0" selected>Seleccione un puesto</option>';

    $.getJSON('Main.php', 'add', function(data) {

        $.each(data, function(index, item) {

            puestos += '<option value="'+item.puesto+'">'+item.puesto+'</option>';    

        });

        $('#jobs').html(uno+puestos);

    });

}

function muestraemp() {

    var empleados = '';

    $.post('Main.php', function(data) {

        $.each(data, function(index, item) {

            empleados += '<tr data-value="'+item.idEmpleado+'"><td>'+item.nombre+'</td><td>'+item.correo+'</td><td>'+item.puesto+'</td><td>lololo</td></tr>';

        });

        $('#admins table tbody').append(empleados);

    }, 'json');

}

function enviar(formemp) {

    $.post('Main.php', formemp, function(data) {

        $('#successadd').show();

        muestraemp();

    });
}

Lightbox is generated dynamically in another script

 var nuevoadmin = $('#addadmin');
 $('#nuevoadmin').on('click', function() {

    lightbox(nuevoadmin);

 });

And this is the url I'm sending data

<?php

include('Empleado.php');

$empleado = new Empleado('localhost', 'root', 'mysql', 'mydb');

if(isset($_GET['add'])) {

    $empleado->listaPuestos();

}

else {

    $empleado->muestraEmpleados();

}

if(isset($_POST['formemp'])) {

    $empleado->agregaEmpleado($_POST['nomadmin'], $_POST['claveadmin'], $_POST['emailadmin'], $_POST['puesto']);
    //$empleado->muestraEmpleados();

}

?>

Thanks in advance.

You are trying to detect form submission using a invalid key or form field.

You have to use any of the form field to detect form submission or simply detect using following code

if(isset($_POST)){
    /* Form submitted using post method process data here... */
}

I solved it. The problem was that I was expecting to fetch the name of the variable that contains the serialized form. I just changed this line:

if(isset($_POST['formemp']))

To this:

if(isset($_POST['nomadmin'], $_POST['claveadmin'], $_POST['emailadmin'], $_POST['puesto'])) 

Anyway, thanks for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM