Javascript/AJAX/PHP error: No element found and unable to write to a file -


i've been struggling error couple of days now. searched stackoverflow , other web sources far no luck.

the code simple. have test.html file calls savefile.php. however, have 2 problems. 1) in firefox, keep getting "no element found" error. don't see problem in chrome. 2) though savefile.php seems getting called, not write file should. case both browsers , need able write file.

this 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 save data");             },      });  </script>  </body> </html> 

this savefile.php:

<?php      $tdata = "hello world test";     $testf = fopen('hello123', 'w+');     fwrite($testf, $tdata);     fclose($testf);      echo "yes";     // header("content-type: text/plain"); got stackoverflow question, didn't fix problem.     exit();  ?> 

both files in same directory under ~/public_html. gave 777 permissions directory. machine running ubuntu 14.04.

in savefile.php, tried "get" instead of "post" no luck again.

i'm stuck , appreciated. thanks.

edit: question not duplicate of 1 linked in comments. main difference not able php write file. don't care "no element found" error long php script able write file. decided include error in post because thought might relevant real problem.

you have specify when ajax event has happen.

since want fire when open page add $(document).ready function , fire when document has finished loading. or can bind example .click() event of 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 save data");                 },         });     }); </script> 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -