Good morning, guys.
I'm starting with PHP, and I'm trying to do some practice tests.
I created an input text and when I press the "ENTER" key it is called an ajax posting this data and calls the file control.php, however in the browser log it is not mentioning the "Success" message of .done
and no "Error" of .fail
, much less the message of controle.php
.
Can you explain what's going on in these scripts?
tela.php
<script src="jquery-3.3.1.js"></script>
<script src="script.js"></script>
<form>
<div class="row">
Campo de Texto:
<input id="id_text" style="height: 25px; font-size:12px; width: 100px" type ="text">
</div>
</form>
script.js
$(document).ready(function(){
var texto = document.getElementById('id_text').value;
$('#id_text').bind("enterKey",function(e){
console.log("Pressionado ENTER "+ texto);
$.ajax({
type: 'POST',
url: "controle.php",
data: {
'texto': texto
}
}).done(function(data) {
console.log("Sucesso");
}).fail(function(data){
console.error("Ajax Error");
});
});
$('#id_text').keyup(function(e){
if(e.keyCode == 13){
$(this).trigger("enterKey");
}
});
});
control.php
<?php
$reqmethod = $_SERVER['REQUEST_METHOD'];
if($reqmethod == 'POST'){
$texto = filter_input(INPUT_POST, "texto");
echo "O texto é: " + $texto;
}else{
echo "Não é um POST";
}
?>