views:

118

answers:

4

I've created a form that is made up of 2 input fields and a wysiwyg text area (ckeditor). I have a function using ajax to gather the ckeditor data to be submitted. I have the form properly submitting to the database, but I also need it to write to a text file. How would I go about doing this?

Edit to include code:

using onclick to submit:

onclick=\"javascript:submitData()\"

ajax function:

function submitData(){
var params='';  
if(document.getElementById('title').value!='' && document.getElementById('date').value!='' && CKEDITOR.instances.article.getData()!=''){
    //build params
    params='&title='+document.getElementById('title').value;
    params+='&date='+document.getElementById('date').value;
    params+='&article='+escape(CKEDITOR.instances.article.getData());
    var httpRequest=new ajaxObject('form.php',processData);
    httpRequest.update('id=submitData'+params);

}

submit to database, then try to submit to flat file:

$saving = $_REQUEST['saving'];
          if ($saving == 1) { 
            $data = $formData['title'];
            $data .= $formData['date'];
            $data .= $formData['article'];

            $file = "/txt/data.txt"; 

            $fp = fopen($file, "a") or die("Couldn't open $file for writing!"); 
            fwrite($fp, $data) or die("Couldn't write values to file!"); 
            fclose($fp); 
            }
A: 

Easiest way would be to have the script invoked via ajax write the data to the text file as well as inserting into the db.

Peter Loron
+1  A: 

I suppose that, somewhere in your PHP script, there is something like

mysql_query("insert into your_table ... ");

that inserts to the database ?

Well, close to that line, you have to write to your file.


The simplest solution I can think about is to use file_put_contents :

file_put_contents('path to your file', $content);

If you just want to create a new file, or override an existing one ; and :

file_put_contents('path to your file', $content, FILE_APPEND);

If you want to add your text at the end of an existing file (and create the file if it doesn't exist).


Of course, you can also use a combinaison of fopen, flock, fwrite, and fclose ; but it means a bit more work ^^

Pascal MARTIN
A: 

Here's what I would do. I'm going to make a few assumptions here about how you're handling the database portion, but you should be able to translate this into working code just fine.

<?php
$wysiwyg_data = $_POST["wysiwyg_data"];
// After you've sent stuff to the DB
$fh = fopen("my_data.txt", "wb");
fwrite($fh, $wysiwyg_date);
fclose($file_handler);
?>

Basically, here's what we're doing:

  1. Grab the data from $_POST (or wherever you're getting it from after you've tossed it in the DB)
  2. Open a text file ("my_data.txt") for writing. If it doesn't exist, it will be created. If you want to control where this file gets created, just pass in an absolute file path
  3. Write the data to the file
  4. Close the file

And your done.

As for the AJAX portion, you would simply pass your data to this script via the sendstring property with the name "wysiwyg_data".

I hope this helps.

Levi Hackwith
that's pretty close to what I was trying, but for some reason it does not write to the text file. i'll edit my question with some of my code so you get a better idea of what i'm trying to do
sassy_geekette
Do you have error_reporting(E_ALL) set? That way, any weird warnings will show up and you may spot something you missed. Try putting error_reporting(E_ALL); at the top of your script and see what you get.
Levi Hackwith
A: 

Hi, I am staggering around like a blind man in the world of PHP,

but I have over come the problem your having, I am using flat files to store dynamic content of a website, html snippets edited in CKeditor and saved as text files, these are then included in each page of the website.

Here is what I have in the Page that contains the CKeditor form.

    <? $contentv = $_GET["contentv"];?><head>
<script type="text/javascript" src="../ckeditor/ckeditor.js"></script>
<form action="1.php?contentv=<? echo $contentv?>" method="post">
<textarea rows="25" cols="70" name="content">
<?
$cext = ".txt";
$files ="../content/";
$fn = $files.$contentv.$cext;
print htmlspecialchars(implode("",file($fn)));
?> 
</textarea>
<br>

</form>
<p>
  <script type="text/javascript">
    CKEDITOR.replace( 'content' );
</script>

  <script type="text/javascript">
    window.onload = function()
    {
        CKEDITOR.replace( 'content' );
    };
</script>
  <?php
$editor_data = $_POST[ 'content' ];
?>
  <script type="text/javascript">
    var editor_data = CKEDITOR.instances.conent.getData();
</script>

Save that as 1form.php and change the addresses to fit your needs or just create a folder called "content" in the same folder as this script and create a text file in that folder called 1.txt

Next you need a file to process the text and save it as a text file

<? $contentv = $_GET["contentv"];?>
<?
$cext = ".txt";
$fn = "./content/".$contentv.$cext;
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
echo "<meta http-equiv=\"refresh\" content=\"0; url=./1form.php?contentv=$contentv\" />\n";
?>

Now save that as 1.php

Text files need to exist in the first instance, as mention before.

Check the path to where you store your files and edit code accordingly

This does use CKeditor so that needs to be on your server as well.

Then you can call the page like this,

http://yourserver.co.uk/1form.php?contentv=1

This way you can call lots of content with 1 form and one saving file.

I have elaborated to control all the content in this way, less strain on server time and makes for easier backup, means you don't need SQL, not that I have a problem with SQL, just another option.

Cheers - Dan Suter - Northampton - UK

Dan Suter