【问题标题】:Save textarea content in file with JS and PHP使用 JS 和 PHP 将 textarea 内容保存在文件中
【发布时间】:2011-10-21 12:02:00
【问题描述】:

我想将其保存在文件中的 textarea 内容有问题。 因此,用户可以在该文本区域中写入一些 HTML、PHP、JS 和/或其他标签,并希望将其保存在文件中。 首先,我在 JS 中声明了一个变量,它接受 textarea 值并通过 ajax 发送到 PHP。 PHP 创建一个文件并在该文件中插入 textarea 内容。

如果我在 textarea 中编写简单的 PHP/HTML/JS 代码,则创建和保存的文件不包含任何内容。它是空的(如果我在 textarea 中使用 PHP 代码)或者有时如果我在 textarea 中编写 HTML 标签。 查看我的脚本:

http://pastebin.com/z6CbSNKC

那么,有什么问题吗?是JS有问题还是PHP代码?

来自 pastebin 的代码:

/* HTML code */
Filename with extension : 
<input type="text" placeholder="ex: test.css" id="titlecopypaste" /><br/>
<textarea cols="60" rows="10" placeholder="Insert code here" id="content"></textarea><br/>
<select id="choose">
<option value="public" selected="selected">Public</option>
<option value="private">private</option>
<option value="both">Public si private</option>
</select>
<center><button id="submitcopypaste">Done</button><br/>
<p id="status_create"></p>
</center>




/* Javascript code */
$("#submitcopypaste").click(function()
{
    var fisier=$("#titlecopypaste").val();
    var content=$("#content").text();
    var select=$("#choose").val();
    $.ajax({
        type:"POST",
        url:"include/uploadtxt.php",
        data:"fis="+fisier+"&cont="+content+"&sel="+select,
        success:function(ev)
        {
            $(this).hide();
            $("#status_create").html(ev);
        }
});





/* PHP code */
session_start();
include('Files.php'); //the class created by
include('connect.php');
$status="";
if(isset($_POST['fis'],$_POST['cont'],$_POST['sel']))
    {
        $obj=new Files();
        $file=$_POST['fis'];
        $content=$_POST['cont'];
        $selected=$_POST['sel'];

        switch($selected)
            {
            case "public":{
                $status=$obj->createfile("../diskuser/_public/".$file, $content);break;
            }
            case "private":{
                if(isset($_SESSION['utilizator'],$_SESSION['parola']))
                    {
                $cer=mysql_query("select * from user where nickname='".$_SESSION['user']."' and password='".$_SESSION['parola']."'");
                if($cer)
                    {
                        while($info=mysql_fetch_array($cer))
                            {
                                $status=$obj->createfile("../diskuser/".$info['nickname']."/".$file, $content);break;
                            }
                    }
                else
                    {
                        $status="Connection error!";
                    }
                    }
                else
                    {
                        $status="The session has been expired !";
                    }
                    break;
            }
            case "both":{
                $status=$obj->createfile("../diskuser/_public/".$file, $content);
                $cer=mysql_query("select * from user where nickname='".$_SESSION['user']."' and password='".$_SESSION['parola']."'");
                if($cer)
                    {
                        while($info=mysql_fetch_array($cer))
                            {
                                $status=$obj->createfile("../diskuser/".$info['nickname']."/".$file, $content);break;
                            }
                    }
                else
                    {
                        $status="Database connection error !";
                    }
                    break;
            }
            }
    }
else
    {
        $status="Invalid data !";
    }
    echo $status;
    });

【问题讨论】:

  • 只需在PHP中打印变量,看看你是否得到了东西,然后你就知道他的问题在哪里了。
  • 你有写文件的权限吗?

标签: php javascript html ajax textarea


【解决方案1】:

我认为您可能需要在发送之前对 textarea 内容进行编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多