Communication between PHP files with Ajax

0

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";
}

?>
    
asked by anonymous 31.10.2018 / 15:14

1 answer

-1

Sorry for the delay in responding.

If you're still experiencing problems, follow code and considerations:

  • In control.php in the concatenation of the phrase there was a '+' as operator. This notation is from the javascript, to make the union between two texts in php the operator '.';
  • The accent gave a problem, so I put a utf8_encode ().
  • In script.js, you brought an alert to display the typed phrase.

follows altered screens for comparison and study:

control.php

<?php
$reqmethod = $_SERVER['REQUEST_METHOD'];

if($reqmethod == 'POST'){
    $texto  = filter_input(INPUT_POST, "texto");
    echo utf8_encode("O texto é: ". $texto);
}else{
    echo "Não é um POST";
}

?>

script.js

$(document).ready(function(){
$('#id_text').bind("enterKey",function(e){
    var texto = document.getElementById('id_text').value;

    console.log("Pressionado ENTER "+ texto);
    $.ajax({
        type: 'POST',
        url: "controle.php",
        data: {
            'texto': texto
        }
    }).done(function(data) {
        console.log("Sucesso" + data);
        alert(unescape(data));
    }).fail(function(data){
        console.error("Ajax Error");
    });
});
$('#id_text').keyup(function(e){
    if(e.keyCode == 13){
        $(this).trigger("enterKey");
    }

});

$(document).on('submit','form',function(e){
    e.preventDefault();
})

});

tela.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="jquery-3.3.1.js" charset="utf-8"></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>
    
05.11.2018 / 16:46