Through a foreach:
foreach ($conexion->query($cefpp) as $row) {
$id = $row['id'];
$dato = $row['dato'];
echo "<a href='plantilla.php?id=".$id."' class="dato" id="dato" data-valor='".$dato."'>";
}
I show several links and I pass a value that varies according to the link.
I want that through ajax and using jquery that data is shown in another part of the page where I have a div, which is where it should be loaded:
<div id="dato" class="dato"></div>
$(document).ready(function() {
$("body").on("click", "a", function(event) {
event.preventDefault();
$.ajax({
url: $(this).attr("href").valueOf()
})
.done(function(data) {
if (data) {
if ($(this).attr("id").valueOf() == "dato") {
var dato = $(this).attr("data-valor").valueOf();
$(".dato").html(dato);
}
else {
alert("Data no tiene valor");
}
})
.fail(function(jqXHR, textStatus, errorThrown) {
if(jqXHR.status === 0) {
alert('Not connect: Verify Network.');
} else if(jqXHR.status == 404) {
alert('Requested page not found [404]');
} else if(jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if(textStatus === 'parsererror') {
alert('Requested JSON parse failed.');
} else if(textStatus === 'timeout') {
alert('Time out error.');
} else if(textStatus === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error: ' + jqXHR.responseText);
}
})
.always(function(result) {
});
});
});
The error returned by the Firefox console is:
TypeError: $ (...). attr (...) is undefined
var dato = $(this).attr("data-valor").valueOf();
If instead this I put - >
var enlace= "." + $(this,"a").prop("class");
var dato = $(enlace).attr("data-valor").valueOf();
Apparently it works, but it actually shows the first result of the foreach.
Why is this happening? What am I doing wrong?