0

I want to create new .txt files but as this code is always returning false, the ajax success function is not executed.

the all code is:

<?php

$nome = $_POST["nome"];
$datanasc = $_POST["datanasc"];
$genero = $_POST["genero"];
$nat = $_POST["nat"];
$morada = $_POST["morada"];
$mail = $_POST["mail"];
$existe = false;
$myFile = "Users.txt";
$myFile1 = "Current_User.txt";



$fh = fopen($myFile, "r")or die("can't open file");
while (($line_of_text = fgets($fh))) {
$Data = explode(';', $line_of_text);
    if($nome == $Data[0] && $datanasc == $Data[1] && $genero == $Data[2] && $nat == $Data[3] && $morada == $Data[4] && $mail == $Data[5]){
        $existe = true;
        break;
    }
}

fclose($fh);
if($existe == true){
$arrayToJs["existe"] = $existe;

}

    else{
        $arrayToJs["existe"] = $existe;

        $fh = fopen($myFile, "a")or die("can't open file");
        $stringData = $nome.";".$datanasc.";".$genero.";".$nat.";".$morada.";".$mail.";"."\n";
        //print_r($stringData);
        fwrite($fh, $stringData);
        fclose($fh);

        $fh1 = fopen($myFile1, "w")or die("can't open file");
        fwrite($fh1, $stringData);
        fclose($fh1);

there is the problem in the code cause is returning false and the ajax success function is not executed. . .

         if((!file_exists($nome.'_Favoritos.txt')) && (!file_exists($nome.'_Cesto.txt'))) {
                $ffav = $nome.'_Favoritos.txt';
                $handle = fopen($ffav, 'w') or die('Cannot open file:  '); 
                fclose($ffav);

                $fcart = $nome.'_Cesto.txt';
                $handle = fopen($fcart, 'w') or die('Cannot open file:  '); 
                fclose($fcart);
        }

    }

echo json_encode($arrayToJs);
?>

Thank you all guys!

0

4 Answers 4

1

Use the file pointer ($handle) you created with fclose:

if((!file_exists($nome.'_Favoritos.txt')) && (!file_exists($nome.'_Cesto.txt'))) {
    $ffav = $nome.'_Favoritos.txt';
    $handle = fopen($ffav, 'w') or die('Cannot open file:  '); 
    fclose($handle);

    $fcart = $nome.'_Cesto.txt';
    $handle = fopen($fcart, 'w') or die('Cannot open file:  '); 
    fclose($handle);
}

Otherwise your file will always return PHP error when those files do not exist

Sign up to request clarification or add additional context in comments.

1 Comment

that was the problem! Thank you very much Syamil MJ!
0

Whether or not your AJAX success function gets called has nothing to do with a PHP code's "return value."

Assuming you're using jQuery or one of the other JavaScript frameworks, it has to do with the HTTP response code. Presumably, you're probably encountering a PHP error which is resulting in a 500 response back to the browser. This would end you up in the error handler instead of the success handler.

Have you tried using something like the network inspector in Chrome (or the Net tab in Firebug) to investigate the actual HTTP response?

1 Comment

Yes I've used the network inspector chrome and php code has no errors. Thank you Colin
0

This code is not returning any value please pass value (true/false) after file created to ajax response.

Comments

0

If you pass a relative path to file_exists, it will return false unless the path happens to be relative to the current PHP directory.

1 Comment

The files are in the same directory .. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.