Send ARRAY Ajax to PHP and insert in BD - Dynamic

0

I have a quantity x of inputs which I want to pass through Ajax to a PHP, the problem is that I pass an Array with all the inputs and their contents, but I want that at the moment of inserting a DB, I insert both the content of the input as the Id, in a synchronized way, I explain id 1 is equal to the value of input 1, etc.

Function Js:

<script>

    function  Guardar_Campos(){
                alert('Funcion Correcta 2');
                idsolicitud = $("#idver").val();
                var nombres_Campos = {};

                $('.mis-ingresos').each(function() {
                    nombres_Campos[$(this).attr('id')] = $(this).val();
                    console.log(nombres_Campos);

                });

                    $.ajax({
                    type : 'POST',
                    data : {'nombres_Campos': JSON.stringify(nombres_Campos),
                            'idsolicitud':idsolicitud},  
                    url : 'Guardar_Campos_Dinamicos.php',
                    success : function(e){

                      console.log(e);

                    }
                    });
              }

</script>  

php:

<?php
include 'Conexion.php';
$object = $_POST['nombres_Campos'];
$array = json_decode(json_encode($object), True);
echo $array;
$Comillas = str_replace('"','', $array);
$Comas = str_replace(',',':', $Comillas);
$Llaves1 = str_replace('{','', $Comas);
$Llaves2 = str_replace('}','', $Llaves1);
$Partes = explode(":", $Llaves2);
echo $Partes[0];
echo "<br>";
echo $Partes[1];
echo "<br>";
echo $Partes[2];
echo "<br>";
echo $Partes[3];

 foreach ($array as  $Value2) {

   var_dump($Value2);

   $Id_Solicitud = $_POST['idsolicitud'];
   $Id_Campo = '2';
   $Signo = '+';

      $procedure_Dinamicos = array(
                    array(&$Id_Solicitud, SQLSRV_PARAM_IN),
                    array(&$Id_Campo, SQLSRV_PARAM_IN),
                    array(&$Desc, SQLSRV_PARAM_IN),
                    array(&$Value2, SQLSRV_PARAM_IN),
                    array(&$Signo, SQLSRV_PARAM_IN)
                  );

                  $sql_Dinamicos = "EXEC SP_INSERT_CAMPOS @Id_Solicitud = ?, @Id_Campo = ?, @Desc = ?, @Valor = ?, @Signo = ?";

                  $stmt_Dinamicos = sqlsrv_prepare($conn, $sql_Dinamicos, $procedure_Dinamicos);

                   if(sqlsrv_execute($stmt_Dinamicos)){

                    echo "Datos Dinamicos Guardados.";

                   }else{

                    echo "Error al Guardar los Dinamicos.";
                   }  

 }    

?>
    
asked by Andres Rodriguez 17.12.2018 в 23:03
source

0 answers