我正在尝试为医药网站创建新闻页面。
我创建了一个新闻文章“创建”页面,允许用户输入作者、描述和正文等信息。
代码如下所示。
<html>
<body>
<style>
textarea
{
resize: none;
}
</style>
<form action="foo.php" method="post">
<label for="textarea">Title:</label>
<p></p>
<textarea rows="1" cols="100" name="title"></textarea>
<br></br>
<label for="textarea">Author:</label>
<p></p>
<textarea rows="1" cols="100" name="author"></textarea>
<br></br>
<label for="textarea">Description:</label>
<p></p>
<textarea rows="15" cols="100" name="desc"></textarea>
<br></br>
<label for="textarea">Body:</label>
<p></p>
<textarea rows="15" cols="100" name="body"></textarea>
<br></br>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>
</body>
</html>
该信息被发送到 foo.php,我希望 foo.php 动态创建一个 div,其中显示标题、作者、发布日期和描述。这个 div 将有一个“阅读更多”按钮,在这种情况下会显示实际的文章或“正文”。
然后我想将这个 div 插入到一个单独的 html 页面中。所以换句话说,每次创建新文章的用户单击提交按钮时,信息都会被处理,添加到 div 元素,然后该 div 被插入到另一个 html 页面。
有什么方法可以做到这一点,或者有一些示例新闻/文章教程可以解释如何做到这一点吗?我已经搜索了几个小时,但似乎没有关于这个特定主题的内容。我也尝试过类似的方法,但这让我无处可去,在尝试解决这个问题 3 天多之后,我准备挖出我的眼睛。
<script>
<?php
foreach (glob("*.txt") as $filename)
{
$lines_array = file($filename);
$search_string_title = "Article_title";
$search_string_published = "Published";
$search_string_author = "Author";
$search_string_desc = "Description";
$search_string_body = "Body";
foreach($lines_array as $line)
{
if(strpos($line, $search_string_title) !== false)
{
list(,$title_str) = explode(":", $line);
}
if(strpos($line, $search_string_published) !== false)
{
list(,$published_str) = explode(":", $line);
}
if(strpos($line, $search_string_author) !== false)
{
list(,$author_str) = explode(":", $line);
}
if(strpos($line, $search_string_desc) !== false)
{
list(,$desc_str) = explode(":", $line);
}
if(strpos($line, $search_string_body) !== false)
{
list(,$body_str) = explode(":", $line);
}
}
$article_title = $title_str;
$published = $published_str;
$author = $author_str;
$desc = $desc_str;
$body = $body_str;
$news_content= array($article_title, $published, $author, $desc, $body);
}
?>
var news = <?php echo json_encode($news_content) ?>;
var title = news[0];
var published = news[1];
var author = news[2];
var desc = news[3];
var body = news[4];
var div = document.createElement("div");
div.style.width = "600px";
div.style.height = "600px";
div.style.background = "red";
div.style.color = "white";
var sHTML = '<div style="text-align:center; padding:15px; font-weight:bold;">' + title + "<br />" + "<br />" + "Published: " + published + "<br />" + "Author: " + author + "<br />" + "<br />" + desc + </div>';
div.innerHTML = sHTML;
div1.appendChild(div);
</script>
最佳答案
您尝试做的事情听起来很像 MVC
我的做法是制作一个类似于您所描述的页面,当我完成输入信息后,我会将数据发送到 PHP 脚本以将我的信息放入数据库(如 MySQL ).
在您希望 div 出现的页面上,我将循环遍历我的数据库文章行,并让我的 php 在这些 div 中回显您想要的信息。
关于javascript - 使用php存储提交信息并在单独的html页面上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24416411/