Returning to PHP generated page after form submission

I have a page generated by PHP eg. the pagetitle is a variable and have a form that likes or dislikes the story. Everytime the user clicks the form at the moment they get returned to the page but without any of the variables generated. I have been experiemting with the REQUEST_URI function but can’t seem to get any results.
The page is generatd by a $_GET request
Here is my controller code:

if (isset($_POST['rate']) and $_POST['rate'] == 'Vote up')
{
    include $_SERVER ['DOCUMENT_ROOT'] . '/includes/db.inc.php';
    $host = $_SERVER['HTTP_HOST'];
    $url = $_SERVER['REQUEST_URI'];
    $storyid = mysqli_real_escape_string($link, $_POST['id']);
    $sql = "UPDATE story SET
    likes = likes +1 WHERE id = '$storyid'";
    $result = mysqli_query($link, $sql);

    if (!$result)
    {
        $error = 'There appears to have been a problem.';
        include 'error.html.php';
        exit();
    }
    header('Location: http://$host$url/story.html.php');
    exit();
    
}

if (isset($_POST['rate']) and $_POST['rate'] == 'Vote down')
{
    include $_SERVER ['DOCUMENT_ROOT'] . '/includes/db.inc.php';
    $host = $_SERVER['HTTP_HOST'];
    $url = $_SERVER['REQUEST_URI'];
    $storyid = mysqli_real_escape_string($link, $_POST['id']);
    $sql = "UPDATE story SET
    dislikes = dislikes +1 WHERE id = '$storyid'";
    $result = mysqli_query($link, $sql);
    
    if (!$result)
    {
        $error = 'There appears to have been a problem.';
        include 'error.html.php';
        exit();
    }
    header('Location: http://$host$url/story.html.php');
    exit();
}

Thank you in advance!

Could you please post the code for the form so we can see how its sending data to this page as you say its using a $_GET request to generate the return request but all i see is $_POST requests

Yeah sure here is the code that generates the page the form is on:

if (isset($_GET['readmore']))
{

include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
$storytitle = mysqli_real_escape_string($link, $_GET['title']);
$id = mysqli_real_escape_string($link, $_GET['id']);
$sql = "SELECT story.id, storytext, storytitle, likes, dislikes, name FROM story INNER JOIN author on authorid = author.id WHERE story.id= '$id'";
$result = mysqli_query($link, $sql);
if (!$result)
{
    $error = 'There was an error retrieving the requested story from the database.';
    include 'error.html.php';
    exit();
}

while ($row = mysqli_fetch_array($result))
{
    $stories[] = array('id' => $row['id'], 'text' => $row['storytext'], 'title' => $row['storytitle'], 'name' => $row['name'], 'likes' => $row['likes'], 'dislikes' => $row['dislikes']);
}
    include 'story.html.php';
    exit();
}

and here is the form as it appears in the page ‘story.html.php’ :

  <form action="?" method="post">
        <input type="hidden" name="id" value="<?php echo $story['id']; ?>" />
        <input type="submit" name="rate" value="Vote up"/>
        <input type="submit" name="rate" value="Vote down"/>
        </form>

basically i whenever i click the form the ‘story.html.php’ page comes back with none of the variables filled and just says undefined variable for everything. i know there’s probably a simple solution but i am a complete php newb

Can you post the exact errors you get please?

I get in the ‘story.html.php’ file:

Notice: Undefined variable storytitle,
Notice Undefined variable stories
and
Warning: Invalid argument supplied foreach().

I will post the whole page if you want.

Thank you

Notice errors are telling you that $storytitle and $stories are not declared yet which is why the error is occurring. Something like the example below will fix it.

$storytitle = isset($_POST['stories']) ? trim($_POST['storytitle']) : false;
$stories = isset($_POST['stories']) ? trim($_POST['stories']) : false;

if ($storytitle && $stories) {
    foreach($stories as $story) {
        // LOOP CODE HERE
    }
}

Thanks for this it appears to be working now except it returns to the index page rather than the 'story.html.php.

The location is set to story.html.php

sorry it is still not working I think I need to do something to use the same URL that created the page.

any ideas?