Hello friend, look, I already modified the html index, here I only modified the open route in the ajax to send it to the server.php, I changed it in the show function and in the update function as you explained to me.
<body>
<div id="wrapper">
<div id="info"> </div>
</div>
<script type="text/javascript">
var resultado= document.getElementById("info");
function mostrarUsuarios(){
var variable;
variable= new XMLHttpRequest();
variable.onreadystatechange= function(){
if(variable.readyState===4 && variable.status===200){
var respuesta= variable.responseText;
resultado.innerHTML=respuesta;
}
}
variable.open("GET","servidor.php?action=mostrar&personas="+"personas",true);
variable.send();
}
// aca afuera ejecutamos la funcion mostrar usuarios
mostrarUsuarios();
function editarUsuario(id_cliente){
var nombreid= "nombreId"+ id_cliente;
var emailid="EmailId"+emailid;
var borrar="borrar"+ id_cliente;
var actualizar="actualizar"+ id_cliente;
var editarnombreid=nombreid+"-editar";
//aca vamos a obtener el nombre de cada persona
var nombredeusuario= document.getElementById(nombreid).innerHTML;
//vamos almacenarlo en una variable padre
var parent= document.querySelector("#"+nombreid);
//esto quiere decir si al darle editar no tiene cajita osea null entonces creamos la cajita
if(parent.querySelector("#"+editarnombreid)===null){
//esto es para que me cree una cajita y yo al darle en editar en la cajita me deja editar los datos
document.getElementById(nombreid).innerHTML='<input type="text" id="'+editarnombreid+'"value="'+nombredeusuario+'">';
//aca inhabilitamos al boton borrar cuando le demos en editar
document.getElementById(borrar).disabled="true";
//vamos a mostrar actualizar usuario este codigo habilita el boton
document.getElementById(actualizar).style.display="block";
}
}
//actualizar
function actualizarusuario(id_cliente){
var variable;
variable= new XMLHttpRequest();
//aca recogemos el id de la cajita
var nombreactualizado = document.getElementById("nombreId" + id_cliente + "-editar").value;
variable.onreadystatechange=function(){
if(variable.readyState==4 && variable.status==200){
mostrarUsuarios();
}else{
document.write("algo malo");
}
}
variable.open("GET","servidor.php?action=editar&usuarioIDActualizado="+ usuarioID + "&nombreActualizado="+ nombreActualizado,true);
variable.send();
}
function borrarusuario(id_cliente){
//aca consultamos si esta seguro de borrarlo y si es si se almacena en respuesta y se hace el comparador y si es no pues no hara nada
var respuesta=confirm("estas seguro de borrra este usuario?");
if(respuesta===true){
var variable;
variable= new XMLHttpRequest();
variable.onreadystatechange= function(){
if(variable.readyState===4 && variable.status===200){
mostrarUsuarios();
}
}
variable.open("GET","servidor.php??usuarioideliminado="+id_cliente,true);
variable.send();
}
}
</script>
</body>
and in this part if I modify most of the code of the server.php as you explained it to me and use the switch structure to do it as it is sent from the index, with the function that it will perform in the switch, but I still get the notice that I get you explained I don't know if I have the code poorly accommodated I am attentive to your comments this is the server.php with what you explained to me it already updates the data in the database, but the notice of the undefined variables continues to appear, but it is updating the database
<?php
include_once "metodo.php";
//creamos las variables de tipo cadena que son as que mostrare en tabla
$nombreId = "nombreId";
$emailId = "emailId";
$actualizar = "actualizar";
$borrar = "borrar";
$mostrarUsarios = new Mostrador();
$mAction = !empty($_GET["action"]) ? $_GET["action"] : null;
/*Si la clave action existe ...*/
if ($mAction) {
/*Trabajaremos con switch, pero puedes hacerlo de otro modo*/
switch ($mAction) {
case "mostrar":
if ($personas === "personas") {
$datos = $mostrarUsarios->mostrar();
$personas = $_GET['personas'];
?>
<table>
<tr>
<th>nombre</th>
<th>correo</th>
<th>actualizar</th>
<th>borrar</th>
</tr>
<?php
foreach ($datos as $dato) {
?>
<tr>
<?php
//aca simplemente se hace comoyo hago solo que se llamo a esas variables para que diga el texto y el id asi correio8 simplemente para no mander el id solo
?>
<td id="<?php echo $nombreId . $dato['id_cliente'] ?>"><?php echo $dato['nombre_cliente']; ?></td>
<td id="<?php echo $emailId . $dato['id_cliente'] ?>"><?php echo $dato['email_cliente']; ?></td>
<?php
// aca le damos un id a cada boton y es el id del usuario
?>
<?php
//luego aca metemos la funcion editar usuaria que la tenemos en el index ?>
<td><input id="<?php echo $dato['id_cliente'] ?>" type="button" onclick="editarUsuario(this.id)" value="editar"></td>
<td><input onclick="borrarusuario(<?php echo $dato['id_cliente']; ?>)" id="<?php echo $borrar . $dato['id_cliente'] ?>" type="button" value="borrar"></td>
<?php // a este le ponemos un estilo oculto por que solo va aparecer si presiono actualizar
//y luego le damos la funcionalidad de actualizar al boton y en la funcion meti codigo php con el id del cliente
?>
<td><input onclick="actualizarusuario(<?php echo $dato['id_cliente']; ?>);" id="<?php echo $actualizar . $dato['id_cliente'] ?>" type="button" value="actualizar" style="display: none;"></td>
</tr>
<?php
}
?>
</table>
<?php
}
?>
<?php
break;
case "editar":
$usuarioidactualizado = $_GET["usuarioidactualizado"];
$nombreactualizado = $_GET["nombreactualizado "];
$datos = new Mostrador();
$datosd = new Mostrador();
if (!empty($nombreactualizado)) {
$con = $this->bd;
$cliente = mysqli_real_escape_string($con, $nombreactualizado);
$resultado = mysqli_query($con, "UPDATE clientes SET nombre_cliente='$cliente' WHERE id_usuario=$usuaridactualizado");
}
$eliminador = new Mostrador();
if (!empty($usuarioideliminado)) {
}
?>
<?php
break;
default:
/*
Aquí haces una operación por defecto cuando haya otro
tipo de action o muestras un mensaje
*/
break;
}
} else {
/*Esto lo puedes cambiar por otra cosa, como un array con una clave error*/
echo "No se posteó una clave action";
}
?>
If you look closely, the code of
servidor.php
is not differentiated. That is, you are sending Ajax requests to that file in two different contexts without differentiating anything on reception. So when you send the request relative topersonas
, the code will try things like this:$_GET['usuarioIDActualizado'];
, but in that case the request doesn't have a keyusuarioIDActualizado
and will therefore skip theNotice
.You can solve this by adding a key in the request that allows you to decide on the server what to do with that request and what data to obtain.
Since the code is long, I will only give you two request examples where we will add a key
action
based on which we will decide on the server what values to look for and what operations to execute.JavaScript/Ajax
PHP
If you analyze the code, you will see that we are dealing with a program that is controlled and consistent with the requests that will be received from the client. Due to the key
action
that we put in Ajax we will know on the server what data will come with that request. That way, no attempt will be made to look up the keyusuarioIDActualizado
in a context in which the key was not sent.I hope it helps you. If there is any problem or doubt, say it in comments to this answer to be notified.