1

I am currently working on a PHP CMS and am trying to create a edit page for a news system.

when the user goes to the edit page and a article has not been selected yet, they should get a form to select an article to edit.

When the user selects an article to edit, the page should (refresh?) display a form with the selected articles content to be edited.


all of my pages are included within my index.php based on p=pagename being set.

When I go to the edit news page I see http://puu.sh/fhYO7/569146d037.png (which is correct).

When I select a news article to edit it sends me to http://puu.sh/fhZ13/2ca0f7d974.png with the url as: mysite.com/admin/editnews.php?id=4

if I manually enter what the URL should be: mysite.com/admin/index.php?p=editnews&id=4 i get this - http://puu.sh/fhZ6h/d38e97d0bb.png

My current code: 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">&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');
    }

    ?>

I am completely lost on this one (just started getting back into PHP after 10+ years away from it).

The Question is: what am I doing wrong, why isnt it sending me to the correct url, why is it displaying strange when I enter what the URL should be?

2
  • so, what's your exact question? Commented Jan 31, 2015 at 19:46
  • 1
    what am I doing wrong, why isnt it sending me to the correct url, why is it displaying strange when I enter what the URL should be? I have been toying with this for hours and havent been able to resolve anything that is wrong with it. Commented Jan 31, 2015 at 19:47

3 Answers 3

2

You have two choices:
1. Get the form to post to index.php instead of editnews.php and take care of the data in index.php. In other words change:

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

to:

<form action="index.php" method="post" autocomplete="off">
  1. Submit the form with ajax without page refresh. This means you have to handle the button click in javascript and process the form with an ajax-call to url editnews.php

In jquery this can be done like so:

$("#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
});
Sign up to request clarification or add additional context in comments.

Comments

1

You should try wrting with a slash at the beginning:

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

Cheers!

Comments

0

I finally figured out a solution (it only took FOREVER)...

First what I did is removed the news select form and just turned the selection into a list of linked titles that would take you to the proper link with &id=#.

Then I changed the check to see if the id was set or not, if so display the form to edit (form was displaying wrong because there was a missing " and ; in my form.

Fixed Code:

<?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');
}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.