Send a generated form with a [While] through POST by Ajax

-2

I have a problem when trying to execute a POST by AJAX, if the function I need to have it inside a [While] that contains a [Form] and I need to modify the dynamic [IDs] of [Script] and [Form], How do I call the function $ (document) .ready (function () {if I can not put it in the head anymore because I have to put it in the while in the body? Do I just remove it? an [ID] in the document, but in this case I have to find all the [IDs] [Submit] that the [While] generates inside the [Form] and I do not know how to put it so that I can execute the post: /

I enclose the principle of the While where it contains the form with its corresponding submits:

            while ($fila=mysqli_fetch_array($result5todos, MYSQLI_ASSOC)){

                echo "
                <table border='3' id='table-ama' >
                    <tr>
                        <form METHOD='POST' id='".$fila[ID]."_formulario_EditarObjeto' accept-charset='utf-8'>
                        <td align='center' class='tdconsulta permisosweb' id='ID' >  
                        <strong style='color:cyan'>ID:</strong> ".$fila[ID]."
                        ";
                if(($UAPrivilegios == 'Moderador') or ($UAPrivilegios == 'Administrador') or ($UAPrivilegios == 'AdminFull')){ echo "
                        <br>
                        <input TYPE='hidden' readonly='readonly'  value='".$fila[ID]."' name='ID'>

                        <input TYPE='hidden' readonly='readonly'  value='".$fila[Tipo]."' name='EditarObjeto_Tipo_Objeto'>
                        <input TYPE='hidden' readonly='readonly'  value='".$fila[Creado_por]."' name='EditarObjeto_Creado_por'>

                        <INPUT TYPE='SUBMIT' onclick='return confirmar2()' id='".$fila[ID]."_btn-EditarObjeto' VALUE='Actualizar' class='boton-azul' >
                        <br>
                ";} 

                if(($UAPrivilegios == 'Administrador') or ($UAPrivilegios == 'AdminFull')){ echo "
                        <br>
                        <input TYPE='hidden' readonly='readonly'  value='".$fila[Nom_Obj]."' name='nom_Objeto'>
                        <input TYPE='hidden' readonly='readonly'  value='".$_SESSION["usuarioactual"]."' name='Eliminar_Usuario'>
                        <INPUT TYPE='SUBMIT' onclick='return confirmar2()' id='".$fila[ID]."_btn-EliminarObjeto' VALUE='Eliminar' class='boton-rojo' style='margin-bottom:4px;'>


                ";} echo "

                        </td>
                "; 

And here I attach the Code where it is seen that the [Form] ends, the scripts and where the [While] ends, also a capture of the [Forms] with the buttons that are generated:

Capture: link

echo "
            </form> 
        </tr>
    </table>";
            ?>
                <script type="text/javascript">
                    //Formulario Editar Objeto _ Envio Ajax
                        $(document).on('ready',function(){
                            $('#<?php echo $fila[ID]; ?>_btn-EditarObjeto').click(function(){
                                var url = "./ajaxpaginaobjeto/Formulario_EditarObjeto.php";                                      

                                $.ajax({                        
                                type: "POST",                 
                                url: url,                    
                                data: $("#<?php echo $fila[ID]; ?>_formulario_EditarObjeto").serialize(),
                                success: function(data)            
                                {
                                    $('#CrearObjeto_Respuesta').html(data);           
                                }
                                });
                            });
                        });
                        //Formulario EliminarObjeto _ Envio Ajax
                            $(document).on('ready',function(){
                                $('#<?php echo $fila[ID]; ?>_btn-EliminarObjeto').click(function(){
                                    var url = "./ajaxpaginaobjeto/Formulario_EliminarObjeto.php";                                      

                                    $.ajax({                        
                                    type: "POST",                 
                                    url: url,                    
                                    data: $("#<?php echo $fila[ID]; ?>_formulario_EditarObjeto").serialize(),
                                    success: function(data)            
                                    {
                                        $('#CrearObjeto_Respuesta').html(data);           
                                    }
                                    });
                                });
                            });
                </script>
            <?php
            } // Fin del While
        ?>

I want to send the POST to PHP by AJAX so that the forms are executed instantly without having to reload the screen, but if I can not get it to work I will have no choice but to send the form in a standard way.

A greeting

    
asked by Samuel Martínez González 23.10.2018 в 18:03
source

1 answer

0

I used this example and it seems to work.

I share it for the one that serves him and I would also like to know if it is well formulated or I have to change something so that the code is well structured:

<head>

<script type="text/javascript">
    $(document).on('click','.btn-EliminarObjeto2',function () {

            // capture input and select value by traversing up into parent(tr),
            // then find element [input, select]
            // this traversing method depend on your table structure
            var Nombre_Objeto = $(this).closest('tr').find('input[name=nom_Objeto]').val(),
                Nombre_Usuario = $(this).closest('tr').find('input[name=Eliminar_Usuario]').val();

            $.ajax({                  
            type: "POST", 
            url: "./ajaxpaginaobjeto/Formulario_EliminarObjeto.php",
            data: {
                ID               : $(this).val(),
                nom_Objeto       : Nombre_Objeto,
                Eliminar_Usuario : Nombre_Usuario,
            },
            success: function (data) {
                $('#CrearObjeto_Respuesta2').html(data);   
            }
        });
    });
</script>

</head>
<body>

    <?php
        while ($fila=mysqli_fetch_array($result6todos, MYSQLI_ASSOC)){
            <table>
                <tr>
                    <td>
                        <input TYPE='hidden' readonly='readonly'  value='".$fila[Nom_Obj]."' name='nom_Objeto'>
                    </td>
                    <td>
                        <input TYPE='hidden' readonly='readonly'  value='".$_SESSION["usuarioactual"]."' name='Eliminar_Usuario'>
                        <button VALUE='".$fila[ID]."' class='btn-EliminarObjeto2' >Eliminar</button>
                    </td>
                </tr>
            </table>
        }
    ?>

</body>
    
answered by 23.10.2018 в 23:40