0
In my PHP page I have the functions: register(), edit() and remove(). How can I make it so that when ajax sends the data in "index.php" falls in the function "registration()"?
AJAX:
$('form[name="cad-form"]').submit(function(){
var dados = $(this).serialize();
$.ajax({
url: '**cadastro.php**',
type: 'post',
data: dados,
success:function(data){
if (data == "sucesso") {
alert("Cliente cadastrado com sucesso! :)");
} else {
alert("Não foi possível realizar o cadastro :( verifique todos os campos ou contate o suporte técnico.")
}
}
})
return false;
})
PHP:
<?php
/* CADASTRO DE CLIENTE */
function Cadastro() {
$nome = $_POST['nome'];
$dataNasc = explode('/', $_POST['data_nasc']);
$dataNasc = $dataNasc[2].'-'.$dataNasc[1].'-'.$dataNasc[0];
$telefone = $_POST['telefone'];
$endereco = $_POST['endereco'];
$cpf = $_POST['cpf'];
$rg = $_POST['rg'];
$conn = new PDO('mysql:host=localhost;dbname=luanaconsolini', 'root', '');
$sql = $conn->prepare("INSERT INTO cliente SET
nome = :nome,
dataNascimento = :data,
telefone = :tel,
endereco = :endereco,
cpf = :cpf,
rg = :rg");
$sql->bindValue(':nome', $nome);
$sql->bindValue(':data', $dataNasc);
$sql->bindValue(':tel', $telefone);
$sql->bindValue(':endereco', $endereco);
$sql->bindValue(':cpf', $cpf);
$sql->bindValue(':rg', $rg);
$sql->execute();
if($sql->rowCount()>0){
echo "sucesso";
}else{
echo "erro";
}
}
Thanks in advance :)
Weslipe, you can put the other functions in your PHP, you say you have register(), edit() and remove(). but I’m only seeing the register(). Do you have options in your HTML to register, edit, or remove? You have to show the code according to the question
– user60252
It does not help to fall into the function "registration()" because it has several details that must be corrected to work correctly
– user60252
Leo, good afternoon to you. i put only the registration function for not too long, but basically I have these 3 functions: register, edit and remove, and ajax sends the form data to "register.php page"
– WesLipe
Right, but how will PHP know if it is sending to register, edit or delete? what is in the form to indicate the function that has to run?
– user60252
That’s my question kk at first, I started my php code already calling the function "Registration()", but I believe there are much better ways to do this, I just don’t know how
– WesLipe
I’m going to put a very basic answer
– user60252