1

I've been struggling with this error for a couple of days now. Searched stackoverflow and other web sources but so far no luck.

The code is fairly simple. I have a test.html file which calls a saveFile.php. However, I have two problems. 1) In Firefox, I keep getting a "no element found" error. Don't see this problem in Chrome. 2) Even though saveFile.php seems to be getting called, it does not write a file as it should. This is the case with both browsers and I really need to be able to write to a file.

This is the test.html file:

<!DOCTYPE html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>IIDS: Basic Boilerplate</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>

hello world

<script src="../components/jquery/jquery.js"></script>

<script type="text/javascript">

    $.ajax({
            type: "POST",
            url: "saveFile.php",
            //url: "http://localhost/~usesr1/mywork/saveFile.php",
            //dataType: "json",
            data: 'test',
            success:function(text) {
                    console.log(text);

            },
            error: function(xhr, status) {
                    alert("unable to save data");
            },

    });

</script>

</body>
</html>

This is the saveFile.php:

<?php

    $tdata = "hello world this is a test";
    $testf = fopen('hello123', 'w+');
    fwrite($testf, $tdata);
    fclose($testf);

    echo "yes";
    // header("Content-Type: text/plain"); Got this from another stackoverflow question, but this didn't fix the problem.
    exit();

?>

Both files are in the same directory under ~/public_html. I gave 777 permissions to the directory. This is a machine running Ubuntu 14.04.

In saveFile.php, tried "GET" instead of "POST" but no luck again.

I'm stuck and any help would be greatly appreciated. Thanks.

EDIT: This question is not a duplicate of the one linked in the comments. The main difference is that I am not able to get my php to write to a file. I don't necessarily care about the "no element found" error as long as the php script is able to write to file. I only decided to include that error in this post because I thought it might be relevant to my real problem.

13
  • Have you watched the request / response in the browser's console? Commented May 14, 2015 at 18:57
  • what happens if you simply access the saveFile.php route in your browser? does the file get created? Commented May 14, 2015 at 18:59
  • Add error reporting to the top of your PHP file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); Commented May 14, 2015 at 19:02
  • Did you search for "firefox no element found"? I found this answer stackoverflow.com/a/976200/4137738 which says: use header('Content-Type: text/plain'); and Firefox will not try to parse the response as XML, and there should be no error. Commented May 14, 2015 at 19:02
  • possible duplicate of Firefox error 'no element found' Commented May 14, 2015 at 19:03

1 Answer 1

1

You have to specify when the AJAX event has to happen.

Since you want it to fire when you open the page add the $(document).ready function and it will fire when document has finished loading. Or you can bind it for example to a .click() event of a button.

<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
                type: "POST",
                url: "saveFile.php",
                data: 'test',
                success:function(text) {
                        console.log(text);   
                },
                error: function(xhr, status) {
                    alert("unable to save data");
                },
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

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.