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.
- The first image shows the text in the Mysql database.
- 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.
- After adding some text to it and pressing spacebar it saves the content, and in Mysql it skips the first line.
- 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!