1
I am making an entry in the database with ajax, ajax esa working, but the insertion is not being done correctly, I believe it is because I am passing the parameters incorrectly in the send();method. I need to pass the name and age in the method. Here is the code:
HTML:
<h1> Cadastre-se em nosso site </h1>
<div id="exibeCont"></div>
<form action="servico.php?p=cadUsr" method="POST" id="frmCadUsr">
Nome: <input type="text" maxlength="255" name="txtNome" id="txtNome"/>
Idade: <input type="text" maxlength="3" name="txtIdade" id="txtIdade"/>
<input type="submit" value="Enviar" />
</form>
the PHP:
function cadUsr(){
require("dbCon.php");
require("mdl_usuario.php");
$usr = $_POST["txtNome"];
$idade = $_POST["txtIdade"];
$resultado = usuario_cadastrar($con,$usr,$idade);
if($resultado){
echo "Cadastro efetuado com sucesso";
} else {
echo "O cadastro falhou";
}
}
PHP function to insert:
function usuario_cadastrar($conexao,$nome,$idade){
if($nome == "" && $idade == ""){
return false;
}
$sql = sprintf("insert into usuario (nome,idade) values ('%s',%s)",$nome,$idade);
$resultado = mysqli_query($conexao,$sql);
return $resultado;
}
And the AJAX:
window.onload = function(){
var xmlhttp;
var frm = document.querySelector("#frmCadUsr");
var url = frm.getAttribute("action");
var nm = document.querySelector("#txtNome").value;
var idade = document.querySelector("#txtIdade").value;
frm.addEventListener("submit",function(e){
e.preventDefault();
try{
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
xmlhttp.open("POST",url,true);
xmlhttp.send(nm+idade);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
alert("Deu certo");
}
}
} catch(err){
alert("Ocorreu um erro.<br />"+ err);
}
});
}
I receive Alert with the message "It worked", but the insertion is not performed.
Where you define this function: $result = user_register($con,$usr,$age); ?
– Sergio
@Sergio, I just updated the code with the function in question !
– anuseranother
Where are you calling
cadUsr()
? gives you some error in PHP?– Sergio
@Sergio, thanks for the help, I found the real problem and I’ve posted the answer !
– anuseranother
possible duplicate of How the variable passes via $_POST
– Guilherme Nascimento
@Guilhermenascimento to be checked by the answer (of my question) is clearly remarkable that this question has nothing to do with mine. My problem was due to the absence of the method xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");, which I was unaware was mandatory, and this is not mentioned at any time in the other question.
– anuseranother
@anuseranother one question being another’s duplicity does not make it "worse" or something like, just "linka" one on the other. Please don’t take this the wrong way.
– Guilherme Nascimento
I do not take, sorry if I was rude, my intention certainly was not that ! I just wanted to explain. Again I’m sorry if I was rude and thank you for your attention !
– anuseranother
@anuseranother was not rude, rest assured. Congratulations on the answer, the tip of the
application/x-www-form-urlencoded
is very important :)– Guilherme Nascimento