I tell you my problem, I am trying to edit a record and the data is called through an id with Js and it works.
But after sending that id with Ajax through "POST"
to a method so that it takes care of performing the query and returns the values, it sends me an error in the method, and the error is in a "if"
but I still don't understand why.
And by the way that when sending the data by ajax, I had dataType:"json"
to comment it because it didn't show me anything.
I leave you the code so you can help me...
users.js (Js and Ajax):
$(document).on("click", ".btnEditarUsuario", function(){
var idUsuario = $(this).attr("idUsuario");
console.log("idUsuario", idUsuario); //Mandando llamar el id con la consola
//AJAX
var datos = new FormData();
datos.append("idUsuario", idUsuario);
$.ajax({
url:"../ajax/usuarios.ajax.php",
method: "POST",
data: datos,
cache: false,
contentType: false,
processData: false,
//dataType: "json",
success: function(respuesta){
console.log("respuesta", respuesta);
}
}); })
And finally, in the final if is where the supposed error that appears in the console is, the only thing I do is through the method to consult the data with the id that I send from the Ajax post and execute it here.
require_once "../controladores/usuarios.controlador.php";
require_once "../modelos/usuarios.modelo.php";
class AjaxUsuarios{
/*=============================================
EDITAR USUARIO
=============================================*/
public $idUsuario;
public function ajaxEditarUsuario(){
$item = "idusuario";
$valor = $this->idUsuario;
$respuesta = ControladorUsuarios::ctrMostrarUsuarios($item, $valor);
echo json_encode($respuesta);
}//Cierra funcion
}//Cierra clase
// EDITAR USUARIO AQUI ES EL ERROR
if(isset($_POST["idUsuario"])){
$editar = new AjaxUsuarios();
$editar -> idUsuario = $_POST["idUsuario"];
$editar -> ajaxEditarUsuario();
}
This is the class I'm calling with the HTML:
<div class="btn-group">
<button class="btn btn-warning btnEditarUsuario"
idUsuario="'.$value["idusuario"].'" data-toggle="modal"
data-target="#modalEditarUsuario"><i class="fa fa-pencil"></i></button>
<button class="btn btn-danger"><i class="fa fa-times"></i></button>
</div>
What it now shows me in the console is the data that I require highlighted in red, the rest is from the HTML:
UPDATE SOLVED
Console, the data I expected to get:
Class:
Try these fixes:
PHP
He
if
is not part of the class. Therefore, the class must close at the end of the functionajaxEditarUsuario
.In any case, it is a code that should be separated, always having your class apart.
JS
Uncomment the
dataType
:the question is that in the controller there is an if outside of a function
require_once "../controllers/users.controller.php"; require_once "../models/users.model.php";