I have a form that’s part of a basic CMS I’ve been developing following a tutorial and I’m after a method that would let a user type what they want, i.e.
"Hello.
Goodbye"
But preserve that carriage return without having to input any code which isn’t particularly user friendly. When I do it by simple submission and then entry into a database it just seems to return it all as one long string.
i guess the returns/new-lines are getting inserted into your database (as new line characters), and i guess you’re retrieving that data and then outputting to a webpage in html. if so, do one of these two things when outputting:
Ok I’ll have a go with that code and see what happens.
For clarification, the process I’m using is thus:
Process content from form and enter into database:
if (isset($_SESSION['userid'])) {
$newsAuthor = mysql_real_escape_string($_SESSION['userid']);
}
if ($_POST['newsTitle']) {
$newsTitle = mysql_real_escape_string($_POST['newsTitle']);
}
else{
echo '<p>The title field is empty.</p>';
}
if ($_POST['newsContent']) {
$newsContent = mysql_real_escape_string($_POST['newsContent']);
}
else{
echo '<p>You must enter something in the content section!</p>';
}
$newsDate = time();
if (isset($newsTitle) && isset($newsContent) && isset($newsAuthor)) {
mysql_query("INSERT INTO news (news_title,news_content,news_created,author_id) VALUES ('$newsTitle','$newsContent','$newsDate','$newsAuthor')") or die(mysql_error);
echo "Your new News post has been successfully submitted.<br/>
Want to make a new News Post? <a href='newnews.php'>Click Here</a>";
}
EDIT: It’s a bit confusing, as far as I can see I’m retrieving them in the same way yet getting two results. This is the CMS area edit form where the info has been called to allow manipulation. The area in the title that I’ve selected doesn’t appear because there’s an apostrophe in it, I had to type that back in.
But this is the public output where the title appears fine but the returns are gone.
the thing to understand is this: new line characters in plain text work. new line characters don’t work, at least not as you want them to, in html.
in that second output in your last post, if you view the source in your browser and look at that text, you’ll see the new lines are in there – as new lines.
new lines (the characters which make new lines) don’t work as new lines in html.
outputting within <pre> tags make the text behave like plain text rather than html therefore you’ll see new lines, the same applies for text within <textarea> tags.
if you want new lines in html, you have to replace the new line characters with <br /> tags which is how you say “new line” in html.
instead of using <br />'s you could wrap the text in paragraph tags appropriately but doing <br />'s is easier.
Ah yes, they are in the source. I tried <PRE> but it made the text really small and it went off the side of the DIV becoming hidden.
I understand a little better what you are saying now, I’ll have a little mess around, either styling the PRE or getting it to insert BR and get back if I have something new. Thanks.
Hmm so I’ve tried the things you suggested (Except PRE, I’m trying to avoid having to add more CSS at the moment) but they don’t ‘appear’ to be working.
Where $newsContent = str_replace(’
‘,’<br/>',$newsContent); is the biggest change. I tried the third line you said as well with array, and both with and without being inside the <p> tags. But it all just appears as a single paragraph.
I’m going to have to try PRE with CSS and see what happens but if you can see what I might be doing wrong here I’d appreciate it. As far as I can tell it should be working unless maybe it isn’t storing the line breaks as
but I don’t think that is how it works.
EDIT: I wonder if its because I’m stripping the slashes from the content before trying to replace the
.
Well I tried with the PHP code and I’m saddened I couldn’t get it to work at all, I don’t know why. I even tried double \ in case it was removing too many accidentally. Had to use PRE with CSS, achieved what I wanted but would rather use your code. Just can’t figure out why it won’t work, its like it won’t find the
or \r.
Sigh, nope, I don’t understand it. Your code looks perfectly fine.
I added in your line to str_replace and tried different combinations of removing the strip_slashes function and putting it back in and adding double-slashes to the str_replace, just doesn’t seem to have any affect on the output at all.
BUT I then added the word ‘Colin’ as that was a part of the content I was testing it on, and it worked, replaced the word Colin with a <br />.
So now I know your line is working…it just seems there are no
or \r for some reason.
This is how it is output at the moment as HTML. Like you said, the lines are there.
<div id='newsBlog'>
<h4>Delivery driver wins £500,000 compensation </h4>
<div class='blogContent'>
<br /> White from Altrincham, Manchester fell from his van while making deliveries. The hired vehicle Mr White and his colleague were using had been fitted with a tail lift, which neither man had any experience with.
While operating the tail lift to gain access to the vehicle storage, Mr White stepped backwards expecting the platform to be in the same position but it had moved, resulting in Mr White falling from the vehicle.
Mr White sustained injuries that left him with temporary paralysis. Following surgery and extensive therapy, he is no longer paralysed but suffers from constant pain and it is unlikely he will be able to return to work. The Judge awarded him half a million pounds as a result of the accident.
<span>Posted Jan 26, 2011 by Mark<br/>
<a href='newsarchive.php'>Read More Claim News</a></span>
</div>
</div>
Ok I’ve managed to fix it using nl2br(). Using that has the line breaks appear. I don’t know why it won’t work the other way but this ‘appears’ to be doing what i want it to. Not sure if there are any downsides to it though.