开发者

Multiple paragraphs in single form field - Jquery append on keydown

I have a very basic CMS that I've put together with PHP and MySQL. Everything works great, except I can't find a way to make textarea fields capable of containing multiple paragraphs (e.g. blog posts, detailed service descriptions). The data is stored in MySQL with <br>s between the paragraphs, but when I call it back for display, all breaks are removed. There is nothing in the PHP that would pull out html (no trimming of html entities, string replacing, etc.) Ideally, I'd be able to wrap the cursor in <p></p> like this textarea I'm typing in now does.

Today I've been trying JQuery.

<script type="text/javascript">
$(document).ready(function(){
$('#thetest').keydown(function(event) {
if (event.keyCode == '13') {
    $('#thetest').append('<p>Test</p&开发者_运维知识库gt;');
});
});
</script>

<form action="test_upload.php" enctype="multipart/form-data" method="POST">
<p>Content: </p>
<textarea wrap="virtual" id="thetest" name="thetest" value="" 
autocomplete="off" rows="20"cols="100">
</textarea>
<input type="submit" name="submit" value="Upload" />
</form>

If anyone could point me in the right direction, I'd greatly appreciate it. I'd think there'd be a jquery plugin for forms that would handle it, but I haven't found one. Thanks very much in advance.


Are your users typing in the paragraph tags, or do you want it WYSIWYG style? If it's the former, you shouldn't have a problem (and if you do, you can just htmlentities() it before it goes in and decode when you fetch it). If it's the latter, I would process the $_POST['thetest'] string as it comes back to you before putting it in your database. You can preg_replace() your newline characters with <p> and </p> before storing it, with more newline characters to make it readable of course.

Or you can cheat with <pre> just to preserve white space, but that wouldn't be semantically responsible :p


Try using nl2br(htmlspecialchars(stripslashes($paragraphs))), it will output the text exactly how the user inputs it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜