gpt4 book ai didi

javascript - 文本编辑器在 <textarea> 中工作,但在显示时不起作用

转载 作者:行者123 更新时间:2023-11-28 07:07:00 25 4
gpt4 key购买 nike

我这里有一个非常非常复杂的问题,我想知道你是否可以提供帮助。

我有一个博客系统,我使用表单来创建博客文章。表单如下所示:http://prntscr.com/7wupnn (正如你所看到的,我做了一点粗体并改变了颜色)。

这是该表单的来源:

<?php
session_start();
include('db_connect.php');
if(!isset($_SESSION['user_id'])){
header('Location: login.php');
exit();
}
if(isset($_POST['submit'])){
$title = $_POST['title'];
$body = $_POST['body'];
$category = $_POST['category'];
$title = $db->real_escape_string($title);
$body = $db->real_escape_string($body);
$user_id = $_SESSION['user_id'];
$date = date('Y-m-d G:i:s');
$body = htmlentities($body);
if($title && $body && $category){
$query = $db->query("INSERT INTO posts (user_id, title, body, category_id, posted) VALUES('$user_id', '$title', '$body', '$category', '$date')");
if($query){
echo '<div style="position:absolute; bottom: 40px; left: 500px; padding: 10px; background: red; box-shadow: 0px 3px 12px 2px #000; color: #fff;">Post Added</div>';
}else{
echo '<div style="position:absolute; bottom: 40px; left: 500px; padding: 10px; background: red; box-shadow: 0px 3px 12px 2px #000; color: #fff;">An unexpected error has occured.</div>';
}
}else{
echo '<div style="position:absolute; bottom: 40px; left: 500px; padding: 10px; background: red; box-shadow: 0px 3px 12px 2px #000; color: #fff;">Please enter all the required information to proceed</div>';
}
}
?>


<div id="mainbox">
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
<input type="text" name="title" placeholder="Enter Post Title Here">
<p></p>
<textarea name="body" id="mytextarea" cols=50 rows=10 placeholder="Enter Post Content Here"></textarea>
<script>
CKEDITOR.replace( 'mytextarea' );
</script>
<p></p>
<input type="file" name="upfile">
<select name="category">
<?php
$query = $db->query("SELECT * FROM categories");
while($row = $query->fetch_object()){
echo "<option value='".$row->category_id."'>".$row->category."</option>";
}
?>
</select>
<p></p>
<input type="submit" name="submit" value="Submit">
</form>
</div>

当我点击“提交”时,该帖子就会发布到博客页面上 - 但是它失去了所有风格 - 这就是我的意思:http://prntscr.com/7wuq9f - 正如您所看到的,浏览器正在显示 HTML,而不是使用它。

这是博客页面的代码:

<?php
include('blog/db_connect.php');
$record_count = $db->query("SELECT * FROM posts");
$per_page = 2;
$pages = ceil($record_count->num_rows/$per_page);
if(isset($_GET['p']) && is_numeric($_GET['p'])){
$page = $_GET['p'];
}else{
$page = 1;
}
if($page<=0)
$start = 0;
else
$start = $page * $per_page - $per_page;
$prev = $page - 1;
$next = $page + 1;
$query = $db->prepare("SELECT post_id, title, LEFT(body, 100) AS body, category FROM posts INNER JOIN categories ON categories.category_id=posts.category_id order by post_id desc limit $start, $per_page");
$query->execute();
$query->bind_result($post_id, $title, $body, $category);
?>

<div id="mainbox">
<?php
while($query->fetch()):
$html_body = nl2br($body);
$lastspace = strrpos($html_body, ' ');
?>
<h2><?php echo $title?></h2>
<p><?php echo substr($html_body, 0, $lastspace)."<a href='blog/post.php?id=$post_id'>..</a>"?></p>
<p>Category: <?php echo $category?>
<hr />
<p></p>
<?php endwhile?>

<?php
if($prev > 0){
echo "<a href='blog.php?p=$prev'>Previous Page</a>";
}
if($page < $pages){
echo "<a href='blog.php?p=$next'>Next Page</a>";
}
?>

</div>

有关如何在发布时保持文本风格的说明,我们将不胜感激。

最佳答案

substr($html_body, 0, $lastspace) 正在 chop 您的 HTML

并且不完整的 HTML 将不会被渲染。

你为什么要这样做?

不要执行substr($html_body, 0, $lastspace)

只需回显$html_body

关于javascript - 文本编辑器在 &lt;textarea&gt; 中工作,但在显示时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31628116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com