Greetings. I need to get the records of a specific row, using ajax I am sending the id to php and I get the row, but I am not being able to manipulate the data to later assign it to html elements. Where will be the error?
$(function() {
$('#id_project').on('change', function()
{
var id_project = $('#id_project').val();
$.ajax(
{
url: "ff.php",
type: "POST",
data:{id_project},
dataType: "html",
success: function(data)
{
var datos = data.nb_informacion_general_localizacion;
alert(datos);
},
})
});
});
PHP
$id_project = $_POST['id_project'];
$sql = "bla bla bla";
while ($row = pg_fetch_assoc($result)) {
echo $row['id_project'];
echo $row['nb_informacion_general_localizacion'];
}
Your problem is that you are returning each variable separately, thus when performing:
you will be sending only the first response (
id_project
) since the AJAX is waiting for the first response that arrives.In order to solve this, I recommend that you enclose the information in an array and encode it as JSON. This way, you will be able to access the data much more easily from your AJAX call.
Your PHP code would look like this:
IMPORTANT:
dataType
of your AJAX function tojson
.header("Content-type: application/json");
to the beginning of your PHP file.