Send post information with Ajax and return separate values

Asked

Viewed 42 times

-1

Here I have a code to open modal sending the collaborator to search in the file cadas.php and return in variables. Example: date.data6, date.Collaborator6, date.Observation6...

Doubt: I wanted to know how I do in php to assign the variables by making a query in the BD of a table and returning the data to the page with the variables.

$(document).on('click', '.edit_data6', function(){  
       var idcolaborador = $(this).attr("Id");  
       $.ajax({  
            url:"cadastro.php",  
            method:"POST",  
            data:{idcolaborador:idcolaborador},  
            dataType:"json",  
            success:function(data){ 

                 $('#data6').val(data.data6);
                 $('#Colaborador6').val(data.Colaborador6); 
                 $('#Observacao6').val(data.Observacao6);
                 $('#Estado1').prop("checked", data.Estado);
                 $('#Conclusao').val(data.Conclusao);
                 $('#idcolaborador').val(data.Id6);
                 $('#insert6').val("Gravar");                    
                 $('#exampleModal6').modal('show');              
            }  
       });  
  });
<?php
$dados = filter_input_array(INPUT_POST, FILTER_DEFAULT);
$Query = mysqli_query($conexao,"SELECT * FROM colaboradores WHERE id='".$dados['idcolaborador']."'")or die(mysql_error()); ?>
while($row=mysqli_fetch_array($Query)){ 
$data6 = $row['data6'];
$Colaborador6 = $row['Colaborador6'];
$Conclusao= $row['Conclusao'];
$Id6= $row['Id6'];} ?>

  • dataType:"json", in AJAX you are expecting a JSON, so you need to make PHP respond with a JSON. Read about the function json_encode of PHP.

1 answer

0

I made this change to PHP, but it’s still not bringing the value in the page with the JS example I mentioned above. What I need is for the values to come separately to use in different id on the page.

<?php
$dados = filter_input_array(INPUT_POST, FILTER_DEFAULT);
$Query = mysqli_query($conexao,"SELECT * FROM colaboradores WHERE id='".$dados['idcolaborador']."'")or die(mysql_error()); 
while($row=mysqli_fetch_array($Query)){ 
  $resultado_dados = array(
    'data6' => $row['data6'],
    'Colaborador6' => $row['Colaborador6'],
    'Observacao6' => $row['Observacao6'],
  );
}

echo(json_encode($resultado_dados));

?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.