将文本和所见即所得表单提交到数据库和平面文件

发布于 2024-08-21 21:47:05 字数 1282 浏览 2 评论 0原文

我创建了一个由 2 个输入字段和一个所见即所得文本区域(ckeditor)组成的表单。我有一个使用ajax 来收集要提交的ckeditor 数据的函数。我已将表单正确提交到数据库,但我还需要它来写入文本文件。我该怎么做呢?

编辑以包含代码:

使用 onclick 提交:

onclick=\"javascript:submitData()\"

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

}

提交到数据库,然后尝试提交到平面文件:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

站稳脚跟 2024-08-28 21:47:05

我想,在你的 PHP 脚本中的某个地方,有类似的东西

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

插入到数据库中?

好吧,在接近该行的地方,您必须写入文件。

我能想到的最简单的解决方案是使用 file_put_contents

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

如果你只是想创建一个新文件,或覆盖现有文件;和:

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

如果您想在现有文件的末尾添加文本(如果文件不存在则创建该文件)。

当然,你也可以使用 fopen, flock, fwritefclose ;但这意味着需要更多的工作^^

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 ^^

天煞孤星 2024-08-28 21:47:05

我在 PHP 的世界里像个盲人一样摇摇晃晃,

但我已经克服了你遇到的问题,我使用平面文件来存储网站的动态内容,在 CKeditor 中编辑的 html 片段并保存为文本文件,这些是然后包含在网站的每个页面中。

这是我在包含 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>

将其另存为 1form.php 并更改地址以满足您的需要,或者仅在与此脚本相同的文件夹中创建一个名为“content”的文件夹,并在该文件夹中创建一个名为 1.txt 的文本文件

接下来,您需要一个文件来处理text 并将其另存为文本文件

<? $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";
?>

现在将其另存为 1.php

文本文件需要首先存在,如前所述。

检查存储文件的路径并相应地编辑代码

这确实使用 CKeditor,因此也需要位于您的服务器上。

然后你可以像这样调用页面,

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

这样您就可以通过 1 个表单和一个保存文件来调用大量内容。

我已经详细阐述了以这种方式控制所有内容,减少服务器时间压力并使备份更容易,意味着您不需要 SQL,并不是 SQL 不好,只是另一种选择。

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 SQL's bad, just another option.

萌面超妹 2024-08-28 21:47:05

最简单的方法是通过 ajax 调用脚本,将数据写入文本文件并插入数据库。

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.

不及他 2024-08-28 21:47:05

这就是我要做的。我将在这里对如何处理数据库部分做出一些假设,但是您应该能够很好地将其转换为工作代码。

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

基本上,这就是我们正在做的事情:

  1. $_POST 获取数据(或者将数据扔进数据库后从任何地方获取数据)
  2. 打开一个文本文件(“my_data.txt”) ”)用于写作。如果不存在,则会创建它。如果您想控制创建此文件的位置,只需传入绝对文件路径
  3. 将数据写入文件
  4. 关闭文件

即可完成。

至于 AJAX 部分,您只需通过名为“wysiwyg_data”的 sendstring 属性将数据传递到此脚本即可。

我希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文