Why is this php code direct from your ajax tutorial with minor changes not working?

I have this php file on my server (and in the manifest) called ajaxPost.php:

    <?php
 // uncomment the next line to see runtime error messages from this script
 ini_set('display_errors','1');

 // Get the data from the client.
 $myText = file_get_contents('php://input');
 $blocknum = "1";
 // Send back the text, reversed
 // echo "Data received from Device:<br>" . $myText;

 //write the data out to a file on the server
 //make sure permissions are all OK!
 $myFile = "AjaxPost/foodblock" . $blocknum . ".txt";
 echo "<p>Writing data to " . getcwd() . $myFile;
 $f = fopen($myFile, 'w') or die("can't open file");
 fwrite($f, $myText);
 fclose($f);
?>

It throws an error every time that it cannot find the file (due to the fopen command I think). Why would this not work? My server is a hostgator shared server and I believe they use suPhp instead of just Php and I did give the folders permissions of 755 and the php file permissions of 644. I’ve also tried it with the exact text from the example and it gave me the same error (so no “foodblock” . $blocknum . “.txt”, just a file named ajaxPost.txt - same exact error.)

The ajaxPost.php file is in the same exact directory as the main project files. What am I doing wrong? I KNOW the nsbasic code is correct from the app - just a simple text string and then exactly what was in the tutorial (with minor changes):

req=Ajax("ajaxPost.php","POST",txtSend.value)
  
  If req.status=200 Then 'success
    NSB.MsgBox(req.responseText)
  Else 'failure
    NSB.MsgBox("Error: " & req.err)
  End If

Please help. I need to write files to the server.

Thanks.

Mike

This is the exact error I get when just running the ajaxPost.php file:

Writing data to /home2/mfelker/public_html/fooddbAjaxPost/foodblock1.txt
**Warning** : fopen(AjaxPost/foodblock1.txt): failed to open stream: No such file or directory in  **/home2/mfelker/public_html/fooddb/ajaxPost.php**  on line  **19**
can't open file

What can I do to fix this?

Mike

Comment out the fopen and instead use echo “variables: “ .print_r($_POST, true); and tell us what you got.

you’re missing a directory. Create it manually via ftp.

Sorry, what would be the full command for the echo?

echo .print_r($_POST, true);

???

Mike

So I was able to figure out how to write my data to the server. Now I need to insert ENTER (carriage return or line feed) since there are a zillion commas, I cannot comma delineate nor is it readable.

How do I put data on a different line (as if pressing the enter key).

This one should be easy but a search comes up with nothing.

Mike

I’ve seen &VbCrLf online, but using basic, how would I add this?

For example, I have the following right now:

txtSend = FullName + " \n " + Address + " \n " + PhoneNum + " \n " + Email + " \n " + “EOF”

Where EOF signals end of file for later reading. I want to have each piece of data be written to a separate line so later I can read it back and put it in an array based on the end of line. The “\n” comes from the ajax way of ending a line. Must be different in nsbasic (basic).

How can I achieve this, please?

Thanks.

Mike

OK, so it looks like the chr command is to be used.

I cannot find a chart for the ASCII equivalent for a LF or CR?

Mike

First thing which comes up when you google

ASCII equivalent for a LF or CR

Yeah. I got it.

It was chr(13)

Now I am good - for now.

Now to read the data back into an array and decrypt.

Mike

You can’t and shouldn’t. Tell us what problem you’re trying to solve.

Not sure what you mean. All is good, for now.

Mike

Mike, what I’m saying is this: If you’re having to transmit CRLF within a text string then I suspect you’re doing something that’s considered bad practice. So, if you tell us more about the problem you’re trying to solve maybe we can suggest an approach that will work better for you in the long term and be easier to implement in the short term.

I understand and appreciate your concern. However, i am writing encrypted data to a file that will be read back later and i want each encrypted field separated by a CR so i can later read it back and put the data into an array. I was going to comma delineate the data but there are many commas in the encrypted strings. That would make it nearly impossible to separate later. In fact, pretty much all symbols, numbers and letters are off limits. That makes the CR the best option.

Mike

If you need help, just ask…

I am trying to read a text file back from the server. The built in text reader command won’t cut it. I do not want the user to have to choose the file.

For now, the filename is specific. In this test, I just want to read it, decrypt it and then put the values back into the text edit boxes.

How do I do this? What command? Do I need another php script to read the file and if so, how do I send it back to nsbasic (or how does nsbasic grab that file’s contents?)

Lastly, I will need to read the resulting text into a array, separating each line into the array - each line ending in a chr(13)

Help is appreciated.

Mike