uso de ajax en la eliminación de una fila
Frecuentes
Visto 56 equipos
1
I have a webpage that has several rows of data coming in mysql database. Im new to ajax i just try it right now, because of the reason that i want to delete row without refreshing the web page. I just want to ask if my code in ajax is correct or did I use it right. here is my code.
this is the generating of rows
mysql_connect("localhost","root","");
mysql_select_db("eis");
$e=0;
$d = mysql_query("select * from hrd_ot");
while($x=mysql_fetch_array($d)){
$e++;
print"<tr id='row".$e."'>
<td>{$x['nam']}</td>
<td>{$x['dep']}</td>
<td>{$x['job']}</td>
<td>{$x['dateofot']}</td>
<td>{$x['frt']}</td>
<td>{$x['tot']}</td>
<td>{$x['toh']}</td>
<td>{$x['app']}</td>
<td><a href='#' onclick=\"return samplexx(".$x['id'].",row".$e.")\">OK</a></td>
</tr>";}
and here is my ajax script | source http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp
<script>
function samplexx(id,row)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
row.style.display="none";
}
xmlhttp.open("GET","sample.php?id="+id,true);
xmlhttp.send();
}
</script>
and here is my sample.php
mysql_connect("localhost","root","");
mysql_select_db("eis");
mysql_query("delete from hrd_ot where id='{$_GET['id']}'");
also it is possible the row that i've choose to delete will delete without using this line of code row.style.display="none";
1 Respuestas
0
Tengo algunas sugerencias.
Instead of using the "GET" method you should use the DELETE method if you are following REST.
It wil be good if you can check for the status of the xmlHttpREquest.
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//hide row
}
row in your samplexx function is not a jquery reference to the selected row. So you should either use
$("#"+row).style.display="none".
or
document.getElementById(row).style.display="none"
The below line just hides the row from the table. So that you dont have to refresh the page and fetch the new list of rows. Do this only if the server returned with 200 status.
$("#"+row).style.display="none";
Respondido 12 Feb 14, 08:02
Sir I think I must read first what is the use of .readyState==4 and .status==200 - Roberto Juan Concepción
Sir can you explain what is 200 status? - Roberto Juan Concepción
Its the HTTP status code. Every response will have a status code. eg: 200 success 404 not found refer this link w3.org/Protocols/rfc2616/rfc2616-sec10.html - Konza
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas javascript ajax or haz tu propia pregunta.
Is your code working? - Konza
yes, at first when i dont put this code row.style.display="none"; the deleted row is remain but when I refresh i it disappear - Robert John Concepcion
Please show how you are calling the
samplexx()
función - andyb