0

Firstly, thanks so much in advance for any help you might be able to give me one this!

Right, what I want to do, is to call a php script to run server side, which takes in a single value (which will be an email) and write it to a text file.

This is the .php file that I want run. I haven't finished it yet, but after hours of trying I can't even seem to get it to create this text file, and write "test"to it.

<html>

<?php
  $myfile = "GatheredEmailAddresses.txt";
  $fopen2 = fopen($myfile, 'a');
  $stringData = "Here";
  fwrite($fopen2, $stringData);
  fclose($fopen2); 
?>
Done!
</html>

So far, I've only made any sort of progress using this. I know it loads the file, because it displays the text "Done!" in the div area called content area, however I just can't figure out why the rest isn't running. This is the jquery I am using(obviously all relevant tags such as etc are included above:

$("#compute").click(function()
    {
       $('#contentArea1').load('test.php');
    });

I'm not even going to bother asking about the email side of things yet, just want to know how I can run this using jquery. Any help would be so greatly appreciated!!

EDIT:

Hi guys,

After trying some stuff this morning, I've identified that it must be a problem with how my php file is being run. When I run it in my IDE, it runs perfectly, creates the file and works fine. However, when I run it using a Jquery post, it simply returns the "TEST TEXT" and displays it in the relevant div. I'm running it using a "load" jquery command. Any ideas? Very frustrated! :(

This is the php file:

<?php
 $dir = 'myDir';
 if ( !file_exists($dir) ) {
      mkdir ($dir, 0777);
 }

 file_put_contents ($dir.'/test.txt', 'Hello File');
?>
TEST TEXT
8
  • Has the text file been created already? Commented Jan 9, 2013 at 16:22
  • Hi Eoin, good thought! Yes it has, and it is not open in notepad or similar so that isn't it either.. Commented Jan 9, 2013 at 16:26
  • 1
    Does php have permissions to write to the file? Commented Jan 9, 2013 at 16:26
  • Hi Ryan, I'm not sure. I'm looking into that now and will let you know, thanks. Commented Jan 9, 2013 at 16:27
  • Have you tried using is_writable() ? Commented Jan 9, 2013 at 16:29

2 Answers 2

1

You could use file_put_contents() instead of all the fopen, fwrite and fclose functions, it would make it much easier to understand :)

Also you need to SEND the data to the PHP file (via AJAX, GET/POST). I can't see you doing that anywhere.

Also - any permissions issues need to be sorted. But I still recommend file_put_contents().

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

3 Comments

Hi Leigh, as I say, before I even attempt that I'd like to know that it runs and at least creates the text file
$handle = fopen($myfile, 'w') or die("can't open file"); 'w' means we want to write :)
I think perhaps you could try getting the PHP part for writing to a file working first (google: PHP write to text file), and once that's working each time you run the page, then you can start doing the jQuery AJAX after.
0

For the jQuery part, you can use $.get

$.get("test.php", { email: "myFirstEmail" );

http://api.jquery.com/jQuery.get/

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.