1
Home: on another page that has HTML, calling a PHP function by Ajax.
In PHP: I call a PHP function using the following code:
//pego o nome da função que foi passada para o campo hidden
@$funcao = $_REQUEST["action"];
/* usei REQUEST porque dependendo do que você for fazer você pode querer enviar via get o nome da função dai ela sera pega do mesmo jeito, porque REQUEST recebe dados via GET, POST, e COOKIE */
if (function_exists($funcao)) {
//call_user_func Chama uma função de usuário dada pelo primeiro parâmetro
call_user_func($funcao);
}
It turns out that when I call the save function, all objects and select
are made again, and this only as bad practice. And if I call the save method before creating the objects, they stay null
.
$conexao = new Conexao();
$cliente = new Cliente();
//Recebe ID do cliente da lista - @ para não dar erro
@$cliente_id = $_GET['cliente_id'];
//pego o nome da função que foi passada para o campo hidden
@$funcao = $_REQUEST["action"];
/* usei REQUEST porque dependendo do que você for fazer você pode querer enviar via get o nome da função dai ela sera pega do mesmo jeito, porque REQUEST recebe dados via GET, POST, e COOKIE */
if (function_exists($funcao)) {
//call_user_func Chama uma função de usuário dada pelo primeiro parâmetro
call_user_func($funcao);
}
function salvar() {
//Variáveis globais
global $conexao, $cliente;
//Variáveis pegando valores passado pelo post
$nome = $_POST['nome'];
$nome_f = $_POST['nome-f'];
//Preenchendo o objeto.
$cliente->setNome($nome);
$cliente->setF($nome_f);
//Salvando no banco de dados
$conexao->saveCliente($cliente);
}
How do I call the save method without calling the objects again and without them being null?
recommend reading the pattern PSR-1
– Pedro Sanção
The amount of bad practices that you use there in this code is huge, starting with the unimportant comments, use of global variables, creation of unnecessary variables, force the
@
as if it solves something and probably the use of the functioncall_user_func
, not to mention that you are looking for another cake recipe that will probably be applied wrong elsewhere, but I don’t understand the problem and where you are going with it. The code is so strange and the description of what it should do is incomplete that I can’t start the help properly..– Maniero
Nor did I mention the inconsistencies, security problems, etc.
– Maniero
I didn’t know my code used so many bad practices. All this is that when by javascript I pass the name of the save function, and the code enters php, I the connection and client variables are null because they were not instantiated. I’m using this because where the html part is I do a include Once and I pass this php page, and through it has all the processes, like show client from a list, save and send to the server.
– Álysson Alexandre
and if you pass an extra parameter in the ajax call, and in php you make decisions according to this parameter?
– Jefferson Silva
The secret in this case: Never trust the customer 100%
– Wallace Maxters
Which parameter would I pass? I didn’t get it right.
– Álysson Alexandre
Some details:
global
= bad practice (install your objects within each function);@
= malpractice (check withisset
and usetry/catch
where necessary); its classConexão
has the methodsave
for each object? = bad practice.– Oeslei
Every time I do a function that uses the client object I’ll have to instantiate? And if I need to use the same client in another role, I won’t have to use the global?
– Álysson Alexandre
Oeslei, I followed your advice and used the $_SESSION, it worked. Thanks.
– Álysson Alexandre
As already said, your code is outside the current standards of good practice programming in PHP, inconsistent and insecure. I suggest you study this reference site with current concepts of good practice: http://br.phptherightway.com/
– Henrique Schreiner
I did not advise to use the
$_SESSION
, is another thing you should avoid. Use the$_SESSION
just for what is really necessary. What I meant was exactly what you imagined: please instate the object in each function that will use it. Using global you can even find problems using the object that has already been used in another function, generating unexpected results.– Oeslei