单个 php 文件中的多个表单


Multiple forms in single php file

我目前正在开发PHP CMS,并正在尝试为新闻系统创建一个编辑页面。

当用户转到编辑页面并且尚未选择文章时,他们应该会获得一个表单来选择要编辑的文章。

当用户选择要编辑的文章时,页面应(刷新?)显示一个表单,其中包含要编辑的所选文章内容。


我的所有页面都包含在我的索引中.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

我当前的代码:编辑新闻.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">&nbsp;</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)。

问题是:我做错了什么,为什么它没有将我发送到正确的 URL,为什么当我输入 URL 应该是什么时它显示奇怪?

您有两个选择:
1. 获取要发布到索引的表单.php而不是编辑新闻.php并处理索引.php中的数据。换句话说,更改:

<form action="editnews.php" method="post" autocomplete="off">

自:

<form action="index.php" method="post" autocomplete="off">
  1. 使用 ajax 提交表单而不刷新页面。这意味着您必须在javascript中处理按钮单击,并使用对url editnews的ajax调用来处理表单.php

在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
});

你应该尝试在开头用斜杠来写:

header('Location: /index.php?p=editnews');

干杯!

我终于想出了一个解决方案(只花了很长时间)......

首先,我所做的是删除新闻选择表单,然后将选择转换为链接标题列表,该列表将带您到&id=#的正确链接。

然后我更改了检查以查看是否设置了 id,如果是,则显示要编辑的表单(表单显示错误,因为缺少"和";在我的表格中。

固定代码:

<?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">&nbsp;</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');
}
?>