-1
I am facing a lot of problems and doubts with ajax! Have the following function that updates the data and list in a table:
function atualiza(){
$.ajax({
dataType: 'json',
url: 'get_enderecos.php',
success: function(data){
for(var i=0; data.length>i;i++){
$('#enderecos').append('<tr><td>'+data[i].sequencia+'</td><td>'
+data[i].cep+'</td><td>'+data[i].endereco+
'</td><td>'+data[i].bairro+'</td><td>'+data[i].cidade+
'</td><td>'+data[i].estado+
'</td><td class="actions col-xs-2 col-sm-2 col-md-2 col-lg-2" align="center">'+
<button class="btn btn-danger" id="teste" type="button">Alterar Endereço</button>);
}
}
});
}
PHP:
<?php
require_once('acessabanco.php');
$objDb = new db();
$link = $objDb->conecta_banco();
$sql = " SELECT sequencia, cep, endereco, bairro, cidade, estado FROM ENDERECOS";
$lista = mysqli_query($link, $sql);
while($resultado = mysqli_fetch_assoc($lista)){
$vetor[] = array_map('utf8_encode', $resultado);
}
echo json_encode($vetor);
?>
HTML:
<table class="table table-striped table-bordered table-hover table-condensed table-responsive">
<thead>
<tr>
<th> Código </th>
<th> CEP </th>
<th> Endereço </th>
<th> Bairro </th>
<th> Cidade </th>
<th> Estado </th>
<th> Alterar </th>
</tr>
</thead>
<tbody id="enderecos">
</tobody>
</table>
I think that this is not the best way (it seems to gambiarra) to list the data in a table, for example: the button I put, I can’t access it, perhaps because it is being declared precisely in a JS function. There is the right way to do this procedure?
The button is outside of javascript, first change
– novic
But how do I get him out? If when I place it generates the following error: Uncaught Syntaxerror: Unexpected token < OBS: '<button>...</button>' for <button>...<button>
– leonardo barussi
missing simple quotation marks '<button class="btn btn-Danger" id="test" type="button">Change Address</button>' does so
– novic
He’s right the way you put it, that’s how it was. However, I cannot find it when I make the following call: $('#test'). on('click',test); which executes the following function: Function test(){ Alert(data.cep); }
– leonardo barussi
looks like it missed closing like after the
</button>' + '</td></tr>';
– novic
'<button class="btn btn-Danger" onClick="test();" type="button">Change Address</button>'+ '</td></tr>' Same problem.
– leonardo barussi
it is more lacked that, something else if has to put the image of that?
– novic
Already on, Perai
– leonardo barussi
The page result image
– novic
I updated the question, give a look... I can’t look at it anymore and I can’t solve it! rs
– leonardo barussi
Posted! From a look ai..
– leonardo barussi
That one
data.length
does not make sense.. Put in question what returns thisdata
... that’s the key to the issue, so your loopfor
doesn’t work– Sam
The change address button which is the probleama?
– novic
A
data.length
would only make sense if you return an array, as it returns a JSON, it makes no sense at all.– Sam
You have to see the structure of this JSON in order to make a loop.
– Sam
my LOOP is working properly, now that Voce said I saw that it does not make much sense, but the problem is not in the is, but in the requisicao what I try to do when I press the CHANGE ADDRESS button. Theoretically he would have to show me a simple Alert: '<button class="btn btn-Danger" id="test 1" type="button">Change Address</button>' $('#teste1'). on('click',test); Function test(){ Alert('a'); }
– leonardo barussi
yes, in the change button.
– leonardo barussi
Puts
$('#teste1').on('click',teste);
within thesuccess
of Ajax at the end– Sam
I managed to solve my friend, thank you very much for the help!!!!!! Always grateful to you guys!
– leonardo barussi