简体   繁体   English

提交文本和所见即所得的形式到数据库和平面文件

[英]Submit text and Wysiwyg form to both Database and flat file

I've created a form that is made up of 2 input fields and a wysiwyg text area (ckeditor). 我创建了一个由2个输入字段和一个wysiwyg文本区域(ckeditor)组成的表单。 I have a function using ajax to gather the ckeditor data to be submitted. 我有一个使用ajax收集要提交的ckeditor数据的功能。 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提交:

onclick=\"javascript:submitData()\"

ajax function: Ajax函数:

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); 
            }

I suppose that, somewhere in your PHP script, there is something like 我想在您的PHP脚本中的某处有类似

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

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 ; 当然,您也可以结合使用fopenflockfwritefclose but it means a bit more work ^^ 但这意味着更多的工作^^

I am staggering around like a blind man in the world of PHP, 在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. 但是我已经解决了您遇到的问题,我正在使用平面文件存储网站的动态内容,在CKeditor中编辑并保存为文本文件的html代码段,然后将这些代码段包含在网站的每个页面中。

Here is what I have in the Page that contains the CKeditor form. 这是包含CKeditor表单的页面中的内容。

    <? $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 将其另存为1form.php并更改地址以满足您的需要,或者仅在与此脚本相同的文件夹中创建一个名为“ content”的文件夹,然后在该文件夹中创建一个名为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 现在将其另存为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. 这确实使用CKeditor,因此也需要在您的服务器上。

Then you can call the page like this, 然后您可以像这样调用页面,

http://yourserver.co.uk/1form.php?contentv=1 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 SQL's bad, just another option. 我已经详细说明了以这种方式控制所有内容的方法,减轻了服务器时间的负担,并使备份更加轻松,这意味着您不需要SQL,不是说SQL不好,而是另一种选择。

最简单的方法是使通过ajax调用的脚本将数据写入文本文件以及插入到db中。

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) $_POST获取数据(或从数据库中获取数据后的任何位置)
  2. Open a text file ("my_data.txt") for writing. 打开一个文本文件(“ my_data.txt”)进行写入。 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". 至于AJAX部分,您只需通过名为“ wysiwyg_data”的sendstring属性将数据传递到此脚本。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM