2

I have a small problem that has been bugging me for quite some time now, but I can't seem to solve it myself. I googled it and didn't really find anything useful. I thought, maybe someone else came across this problem, or will come across it in the future, so we could help each other.

The problem; I have made a dropdown list, containing projects. Under this dropdown list there is a textarea. When the user selects a project from the dropdown list, its content is loaded in to the textarea. When I add some text to the textarea, it is automatically saved whenever I press the spacebar or enter. The problem is however, that PHP seems to add 2 linebreaks BEFORE the text. I made some screenshots to illustrate this.

  1. The first image shows the text in the Mysql database.
  2. You can see that it somehow skips the first 2 lines when displaying the content in a textarea. The notepad look is just a background picture I set within the textarea.
  3. After adding some text to it and pressing spacebar it saves the content, and in Mysql it skips the first line.
  4. In the content textarea it skips a line as you can see in the image.

Screenshots: http://s9.postimg.org/suh6u3a7j/1234.png

Does anyone know why this is happening? And how can I solve it?

Here is is some code;

<script>
  $(function(){
  $("textarea[id='projectcontent']").keypress(function(e) {
      //13 maps to the enter key
      if (e.keyCode == 13 || e.which == 13 || e.keyCode == 32 || e.which == 32 || e.keyCode == 8 || e.which == 8) {
          saveContent();
      }
  });
});

<script>
        function getContent()
        {
            var mail = $("#email").val();
            var project = $("#projects").val();

            $.ajax({
                type : "POST",
                url : "/includes/projects.php",
                data : "project=" + project+  "&email=" + mail+ "&mode=get",
                success : function(result)  {  $("#projectcontent").val(result);  }
            });
        }
    </script>

<script>
        function saveContent()
        {
            var mail = $("#email").val();
            var project = $("#projects").val();
            var projectcontent = $("#projectcontent").val();

            $.ajax({
                type : "POST",
                url : "/includes/projects.php",
                dataTyp: 'html',
                data : "project=" + project+  "&email=" + mail+ "&projectcontent=" +projectcontent+ "&mode=save",
                success : function(result)  {  $("#response").val(result);  }
            });
        }
    </script>

And the drop down and textarea

<div id="project">
        <?php
        $projects = new project();
        $projects = $projects->getProjects($_SESSION['user']);?>
        <form><select id="projects" onchange="getContent()"><option>Select project</option>
            <?php
            foreach($projects as $project)
            {?>
                <option value="<?php echo $project['id'];?>"><?php echo $project['name']; ?></option>
            <?php
            }
            ?>
        </select><input type="hidden" id="email" value="<?php echo $_SESSION['user']; ?>" /></form><span id="response"></span><br /><textarea id="projectcontent" onkeydown="enterkey()"></textarea>
        </div>

And the PHP code to save the content.

//set project content
public function setProjectContent($projectid, $email, $projectcontent) {
    $spc    =   $this->db->prepare('update projects set content=? where id=? and owner=?');
    $spc->execute(array(trim($projectcontent, ""), $projectid, $email));
    if ($spc->rowCount()==1){
        $message = "Saved";
    }
    else {$message = "Not saved";}
    return $message;
}

And the PHP code to get the content.

//get project content
public function getProjectContent($projectid, $email) {
    $this->projectid= $projectid;
    $this->email = $email;
    $gpc    =   $this->db->prepare('select content from projects where id=? and owner=?');
    $gpc->execute(array($projectid, $email));
    $result =   $gpc->fetch();
    $content = $result['content'];
    return $content;
}

EDIT: Ok, problem is solved. This may be of use to someone else; The problem was simply that I had some white spaces in one of the include files. Removing the white spaces solved this problem!

4

2 回答 2

3

远射,但我认为您的 PHP 代码(具有 AJAX 功能或其中之一的代码)可能在<php标签(或其他地方)前面有白线。

更改 ajax 代码调用的 php 方法,使其返回硬编码值。当你再次测试它时,你可以确定它不是内部逻辑,看看线条是否还在。例子:

//get project content
public function getProjectContent($projectid, $email) {
    $this->projectid= $projectid;
    $this->email = $email;
    $gpc    =   $this->db->prepare('select content from projects where id=? and owner=?');
    $gpc->execute(array($projectid, $email));
    $result =   $gpc->fetch();
    $content = $result['content'];
    return "test on first line";
}

编辑:对于在 php 文件中寻找空格的人,我制作了一个可以找到它们的正则表达式(\s\<\?)|(\?\>\s)

于 2013-06-01T17:05:40.497 回答
1

$("#projectcontent").val(result);这是将值设置为 textarea 的代码,对吗?然后你可以用innerHtml()js函数代替。此外,在保存 textarea 内容时,您可以执行$.trim($var)修剪功能。

于 2013-06-01T17:27:08.697 回答