2

Hello guys that is not normal :) !!

foo.php

     <?php 
         if (isset($_POST['data']))
         $stringData = $_POST['data'];
         $file = "ciao.txt"; 
         $fh = fopen($file, 'w') or die("can't open file");
         fwrite($fh, $stringData);
         fclose($fh); 

         ?>

file.js

    function WriteToFile() {
        var dataa = "foo bar";
     $.post("foo.php", {data: dataa}, function(result){ alert("ciaoooo!!");}            
           , "json");
    }

this is the error and i can't write on my file.txt

Notice: Undefined variable: stringData

I tried also with that kind of function

function WriteToFile() {
    var data = "foo bar";
$.ajax({
    url: 'foo.php',
    type: 'POST',
    data: { 'data' : 'data'},
    success: function() {
        alert('the data was successfully sent to the server');
    }
});

but the result is the same!! Anyone have some idea???
2
  • you shouldn't have data in quotes as the value of the data property, or it will just literally pass the string 'data' Commented Jul 27, 2012 at 17:06
  • If the $stringData variable is not defined. That means that the isset($_POST['data']) conditional did not execute, which means you have no post data. Commented Jul 27, 2012 at 17:08

4 Answers 4

4

You're missing curly brackets:

if (isset($_POST['data'])) {
         $stringData = $_POST['data'];
         $file = "ciao.txt"; 
         $fh = fopen($file, 'w') or die("can't open file");
         fwrite($fh, $stringData);
         fclose($fh); 
 }

Without them, you essentially have this:

 if (isset($_POST['data'])) {
         $stringData = $_POST['data'];
 }
 $file = "ciao.txt"; 
 $fh = fopen($file, 'w') or die("can't open file");
 fwrite($fh, $stringData);
 fclose($fh); 

Which explains why you're getting undefined $stringData, because the POST is not being executed properly.

Note that this doesn't solve your JS / jQuery problems. For that, see the other answers.

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

Comments

2

Ok, I think here's what is happening:

The code you have posted (foo.php/file.js in example 1) is correct, and will work without issues. I am not sure if you are trying to hit the foo.php URL directly in the browser. In that case there is nothing posted, so $stringData will be undefined, and it will throw the notice you are seeing.

What you need to do is: 1. Include the file.js script in an HTML file. 2. Make sure you have included jquery library 3. Make sure that the PHP (foo.php) file path is correct in $.POST 4. Call WriteToFile() on the HTML body onLoad function

Here is a sample HTML which should work (update path to foo.php if needed)

<script src ="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script>
function WriteToFile() 
{
    var dataa = "foo bar";
    $.post("foo.php", {data: dataa}, function(result){ alert("ciaoooo!!");}, "json");
}
</script>

4 Comments

i did all the 4 steps that you said before but is still not working, is like if the value data that i send by POST method is not formatted in a good way!!
hmm...strange, cause I copy-pasted the very same code sample you had and it works for me. Can you paste all three file codes you have, the PHP,JS AND the HTML used to call it? I think this is something really small that we are missing..
I made some test and i discovered that is like when i open the file.html he doesn't call the php file, because i can write on ciao.txt only if I launch the php file
Hey i looks like when i test it work in local but when i deploy it on the serever doesn't want to work....
0

Don't use quotes at this point:

data: { 'data' : 'data'},

replace it with:

data: {data : data},

Also, in the first code snippet you need brackets, correct it like this:

if (isset($_POST['data'])) {
  /* Your code here */
}

Comments

0

I think you want something like this:

function WriteToFile() {
    var data = "foo bar";
    $.ajax({
    url: 'foo.php',
    type: 'POST',
    data: data,
    success: function() {
        alert('the data was successfully sent to the server');
    }
});

1 Comment

I tried boths of the solution but i still have the same problem but if i check $_POST with if (isset($_POST['data'])) i dont have the NOTICE but i can't write into the file.

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.