1
Good morning. I want to make the result of the variable Cpf is displayed in the input 'gets'.
practicing.php
<head>
<meta charset="UTF-8"/>
<script type="text/javascript" src="pratica.js"></script>
</head>
<body>
<fieldset style="width:50%; margin: 0px auto; ">
<legend>Colocando PONTO no CPF</legend>
<form id="form">
<label for="cpf">CPF</label>
<input type="number" name="cpf" placeholder="Sem pontos e traço" required />
<input type="submit" value="ENVIAR" name="button"/><br/><br/>
<input name='recebe' id="recebe" readonly style='width:100%;'/>
</form>
</fieldset>
</body>
validationPraticando.php
$cpf = $_POST['cpf'];
if(strlen($cpf) == 11){
$pegaCpf = substr($cpf,0,3).'.'.substr($cpf,3,3).'.'.substr($cpf,6,3).'-'.substr($cpf,9,2);
echo json_encode($pegaCpf);
}else{
echo json_encode("CPF Invalido");
}
js practice.
$(documento.ready(function(){
$("#form").on("submit",function(e){
e.preventDefault();
var data = $("#form").serialize();
$.ajax({
url: "validaPraticando.php",
data: data,
method: "POST",
dataType: "json",
success: function(data){
$("#retorno").val(data);
},
error: function(){
alert("erro na requisição");
}
});
});
});
Check in the browser what happens when you call the page validatingPraticando.php, in the Success of an Alert(data); just to check if it is returning correctly.
– Caique Romero
Only by complementing what @Caiqueromero said, instead of Alert(data) put console.log(data) and press F12 to see the result on the console, because if it is an object or an array will not show the values using Alert.
– Wictor Chaves
There is no element with the id "return" in your html.
– Victor Eyer
change $("#return"). val(date); for $("#receive"). val(date);
– Diogo Henrique Fragoso de Oliv
The browser is warning this error "Uncaught Syntaxerror: Missing ) after argument list". Refers to line 23 of the Javascript file. That line is the last one "});".
– Guilherme Wayne