我目前正在开发一个PHP CMS,我正在尝试为新闻系统创建一个编辑页面。
当用户进入编辑页面并且尚未选择文章时,他们应该获得一个表单来选择要编辑的文章。
当用户选择要编辑的文章时,页面应该(刷新?)显示包含要编辑的所选文章内容的表单。
我的所有网页都包含在我的index.php中,基于p = pagename设置。
当我进入编辑新闻页面时,我看到http://puu.sh/fhYO7/569146d037.png(这是正确的)。
当我选择要编辑的新闻文章时,请将我发送到http://puu.sh/fhZ13/2ca0f7d974.png,其网址为:mysite.com/admin/editnews.php?id = 4
如果我手动输入URL应该是什么:mysite.com/admin/index.php?p=editnews&id=4我得到了这个 - http://puu.sh/fhZ6h/d38e97d0bb.png
我当前的代码: editnews.php
<?php
ob_start();
session_start();
include_once('includes/connection.php');
include_once('includes/news.php');
$news = new AdminNews;
if (isset($_SESSION['logged_in'])) {
$newss = $news->fetch_all();
if (isset($_POST['title'], $_POST['content'])) {
$title = $_POST['title'];
$content = br2nl($_POST['content']);
if (empty($title) or empty($content)) {
$error = 'All fields are required!';
header('Location: index.php?p=editnews');
} else {
global $pdo;
$query = $pdo->prepare('UPDATE admin_news SET news_title = ?, news_content = ? WHERE news_id=?');
$query->bindValue(1, $title);
$query->bindValue(2, $content);
$query->bindValue(3, $id);
$query->execute();
header('Location: index.php');
}
}
if (isset($_GET['id'])) {
$id = $_GET['id'];
?>
<!-- POST -->
<div class="post">
<div class="topwrap">
<div class="userinfo pull-left">
<div class="avatar">
<img src="images/avatar.jpg" alt="" />
<div class="status green"> </div>
</div>
<div class="icons">
<img src="images/icon1.jpg" alt="" /><img src="images/icon4.jpg" alt="" /><img src="images/icon5.jpg" alt="" /><img src="images/icon6.jpg" alt="" />
</div>
</div>
<div class="posttext pull-left">
<h2>Edit News</h2>
<?php if (isset($error)) { ?>
<small><?php echo $error; ?></small>
<?php } ?>
<!-- add news form start !-->
<form action="editnews.php" method="post" autocomplete="off">
<input type="text" name="title" value="<?php echo $news['news_title'] ?> /><br /><br />
<textarea rows="10" cols="87" value="<?php echo $news['news_content'] ?>" name="content" /></textarea>
<!-- add news form break !-->
</div>
<div class="clearfix"></div>
</div>
<div class="postinfobot">
<div class="dateposted pull-right">
<!-- add news form continue !-->
<input class="btn btn-primary" type="submit" value="Submit Changes" />
</form>
<!-- add news form end !-->
</div>
<div class="clearfix"></div>
</div>
</div>
<!-- POST -->
<?php
} else {
?>
<!-- POST -->
<div class="post">
<div class="topwrap">
<div class="userinfo pull-left">
</div>
<div class="posttext pull-left">
<h2>Select a News Post to Delete</h2>
<!-- form start !-->
<form action="editnews.php" method="get" autocomplete="off">
<select name="id">
<?php foreach ($newss as $news) { ?>
<option value="<?php echo $news['news_id']; ?>">
<?php echo $news['news_title']; ?>
</option>
<?php } ?>
</select>
<!-- form break !-->
</div>
<div class="clearfix"></div>
</div>
<div class="postinfobot">
<div class="dateposted pull-right">
<!-- form continue !-->
<input class="btn btn-primary" type="submit" value="Edit News" />
</form>
<!-- form end !-->
</div>
<div class="clearfix"></div>
</div>
</div>
<!-- POST -->
<?php
}
} else {
header('Location: index.php');
}
?>
我完全迷失在这一点上(距离它已经10年多了之后才重新开始使用PHP)。
问题是: 我做错了什么,为什么它不会把我发送到正确的网址,为什么当我输入网址时它会显得很奇怪?
答案 0 :(得分:2)
您有两种选择:
1.获取表单以发布到index.php而不是editnews.php并处理index.php中的数据。换句话说改变:
<form action="editnews.php" method="post" autocomplete="off">
为:
<form action="index.php" method="post" autocomplete="off">
在jquery中,可以这样做:
$("#myForm").submit(function() {
$.post('editnews.php',
{
title: $('#name').val(),
content: $('#content').val()
},
function(data) {
console.log(data); //response
$('#name, #content').val(''); /* Clear the inputs */
},
'text');
return false; //Stop form from refreshing page
});
答案 1 :(得分:1)
你应该尝试在开头写一个斜杠:
header('Location: /index.php?p=editnews');
干杯!
答案 2 :(得分:0)
我终于想出了一个解决方案(它只用了FOREVER)......
首先我所做的是删除了新闻选择表单,只是将选择内容转换为链接标题列表,这些标题会将您带到与&amp; id =#的正确链接。
然后我更改了检查以查看是否设置了ID,如果是,则显示要编辑的表单(表单显示错误,因为缺少“和;在我的表单中。” / p>
固定代码:
<?php
ob_start();
session_start();
include_once('includes/connection.php');
include_once('includes/news.php');
include_once('includes/functions.php');
$news = new AdminNews;
$funct = new UserFunctions;
if (isset($_SESSION['logged_in'])) {
$newss = $news->fetch_all();
if (isset($_POST['title'], $_POST['content'])) {
$title = $_POST['title'];
$content = nl2br($_POST['content']);
if (empty($title) or empty($content)) {
$error = 'All fields are required!';
header('Location: index.php?p=editnews');
} else {
global $pdo;
$query = $pdo->prepare('UPDATE admin_news SET news_title = ?, news_content = ? WHERE news_id=?');
$query->bindValue(1, $title);
$query->bindValue(2, $content);
$query->bindValue(3, $id);
$query->execute();
header('Location: index.php');
}
}
if (isset($_GET['id'])) {
$id = $_GET['id'];
$query = $pdo->prepare("SELECT * FROM admin_news WHERE news_id = ?");
$query->bindValue(1, $id);
$query->execute();
$rows = $query->fetchAll();
//set username to variable
foreach ($rows as $row) {
$id = $row['news_id'];
$title = $row['news_title'];
$content = $funct->br2nl($row['news_content']);
}
?>
<!-- POST -->
<div class="post">
<div class="topwrap">
<div class="userinfo pull-left">
<div class="avatar">
<img src="images/avatar.jpg" alt="" />
<div class="status green"> </div>
</div>
<div class="icons">
<img src="images/icon1.jpg" alt="" /><img src="images/icon4.jpg" alt="" /><img src="images/icon5.jpg" alt="" /><img src="images/icon6.jpg" alt="" />
</div>
</div>
<div class="posttext pull-left">
<h2>Edit News</h2>
<!-- add news form start !-->
<form action="editnews.php" method="post" autocomplete="off">
<input type="text" name="title" value="<?php echo $title; ?>" /><br /><br />
<textarea rows="10" cols="87" name="content" /><?php echo $content; ?></textarea>
<!-- add news form break !-->
</div>
<div class="clearfix"></div>
</div>
<div class="postinfobot">
<div class="dateposted pull-right">
<!-- add news form continue !-->
<input class="btn btn-primary" type="submit" value="Submit Changes" />
</form>
<!-- add news form end !-->
</div>
<div class="clearfix"></div>
</div>
</div>
<!-- POST -->
<?php
} else {
?>
<!-- POST -->
<div class="post">
<div class="topwrap">
<div class="userinfo pull-left">
</div>
<div class="posttext pull-left">
<h2>Select a News Post to Delete</h2>
<?php foreach ($newss as $news) { ?>
<?php echo $news['news_id']; ?> - <a href="index.php?p=editnews&id=<?php echo $news['news_id']; ?>"><?php echo $news['news_title']; ?></a><br /><br />
<?php } ?>
</div>
<div class="clearfix"></div>
</div>
<div class="postinfobot">
<div class="dateposted pull-right"> </div>
<div class="clearfix"></div>
</div>
</div>
<!-- POST -->
<?php
}
} else {
header('Location: index.php');
}
?>