Why does not the variable I want in PHP come out of the console?

0

Greetings, I have this JS code where I use AJAX ($ ("# pass") is the password of the form):

    function egiaztatuPasahitza(){
        var data = $("#pass");
        var request = $.ajax({
            url:'egiaztatuPasahitza.php',
            data: data,
            dataType:'html'
        });
        request.done(function(data){
            if(data == true) {
                alert("true");
             } else {
               alert("false");
            }
        });
        request.fail(function(data){

        });
    };

Who calls this PHP code:

$pasahitzak = fopen('../DATUAK/toppasswords1.txt','r');
$pass = $_REQUEST[data];
while ($linea = fgets($archivo)) {
    $aux[] = $linea;    
    $numlinea++;
    echo "<script>console.log( 'Pasahitza: " . "$pass" . "' );</script>\n";
    if("$pass"=="$linea"){
        echo "<script>console.log( 'Pasahitza: " . "$pass" . "' );</script>\n";
        echo "true";
    }else{
        echo "false";
    }
}
fclose($pasahitzak);

This code reads a file line by line to compare it with a password that comes from a form.

My problem is that when making lines like this:

echo "<script>console.log( 'Lerroa: " . "$linea" . "' );</script>";

The console does not read it and only the information of the request comes out like this (there is nothing more than the drop-down even if it closes it):

I think there is nothing wrong in the code but I do not know why it does not register it in the console. I tried to change it for an alert () and neither does it. Greetings.

    
asked by Mikel Molinuevo 30.11.2017 в 23:32
source

1 answer

2

The question is how it was mentioned in comments so it does not work through ajax, since everything you do with echo is a response to ajax

Counting your previous question you need the true and other values, you can try this

$pasahitzak = fopen('../DATUAK/toppasswords1.txt','r');
$pass = $_REQUEST[data];
$response = ['valid' => 'false', 'pass' => NULL];
while ($linea = fgets($archivo)) {
    $aux[] = $linea;    
    $numlinea++;
    if($pass==$linea){    
        response['valid'] = "true";
        response['pass'] = $pass;
        break;
    }
}
fclose($pasahitzak);
echo json_encode($response);

Now in your js you can display the values in the done

request.done(function(data){
  if(data.valid == true) {
    alert("la contraseña correcta fue" + data.pass );
  } else {
     alert("false");
  }
});
    
answered by 30.11.2017 / 23:54
source