简体   繁体   中英

Why won't my page refresh?

I have a small time slot booking system, where I can click a link called: Reserve, and then I reserve that given time.

However, the page doesn't refresh after I've clicked on reserve. Therefore it's possible for a user to click the same reserve link twice. Whitch they shouldn't be able to.

if (isset ( $_GET ['reserved'] )) { 
    $sqlreserve = "INSERT INTO calendar (eventDate, timeslot) VALUES ('" . $dateToCompare . "','" . intval($_GET['t']) . "');";
    $resultreserve = mysqli_query ( $mysqli1, $sqlreserve );
    if ($resultreserve) {
        header('Location: '.$_SERVER['PHP_SELF']);
    } else {
        echo "Event Failed to add";
    }
}

If my insert works, then I call: header('Location: '.$_SERVER['PHP_SELF']);

I'm working on localhost, if that has anything to say?

EDIT:

The way I create my links and the text saying that a slot is booked is like this:

    if (mysqli_num_rows ( $result ) == 0) {
        echo "<a href='" . $_SERVER ['PHP_SELF'] . "?month=" . $month . "&day=" . $day . "&year=" . $year . "&t={$time}&v=true&f=true&reserved=true'><h3 style='color: rgb(255,0,0);'>Reserve</h3></a>";
    } else {
        echo "<h3>Not Available, taken by:</h3>";
        while ( $row = mysqli_fetch_array ( $result ) ) {
            echo "<br />";
        }

}

EDIT. My Error:

Cannot modify header information - headers already sent by (output started.....)

for($i = 1; $i < $numDays; $i ++, $counter ++) {
                $timeStamp = strtotime ( "$year-$month-$i" );
                if ($i == 1) {
                    $firstDay = date ( "w", $timeStamp );
                    for($j = 0; $j < $firstDay; $j ++, $counter ++) {
                        echo "<td>&nbsp;</td>";
                    }
                }
                if ($counter % 7 == 0) {
                    echo "</tr><tr>";
                }
                $monthstring = $month;
                $monthlength = strlen ( $monthstring );
                $daystring = $i;
                $daylength = strlen ( $daystring );
                if ($monthlength <= 1) {
                    $monthstring = "0" . $monthstring;
                }
                if ($daylength <= 1) {
                    $daystring = "0" . $daystring;
                }

                $todaysDate = date ( "m/d/Y" );
                $dateToCompare = $monthstring . '/' . $daystring . '/' . $year;
                echo "<td align='center' ";
                if ($todaysDate == $dateToCompare) {
                    echo "class='today'";
                } else {
                    $sqlCount = "SELECT * FROM calendar WHERE eventDate='" . $dateToCompare . "'";
                    $noOfEvent = mysqli_num_rows ( mysqli_query ( $mysqli1, $sqlCount ) );
                    if ($noOfEvent >= 1) {
                        echo "class='event'";
                    }
                }
                echo "><a href='" . $_SERVER ['PHP_SELF'] . "?month=" . $monthstring . "&day=" . $daystring . "&year=" . $year . "&v=true'>" . $i . "</a></td>";
            }

The line affected is:

echo "><a href='" . $_SERVER ['PHP_SELF'] . "?month=" . $monthstring . "&day=" . $daystring . "&year=" . $year . "&v=true'>" . $i . "</a></td>";

It is in another file where I have my calendar, in which I have links to the specific day that I wan't to book the timeslots fore:

在此处输入图片说明

Try this..

header('Location: '.$_SERVER['PHP_SELF']);
exit;

I think your code is continuing...

A more in-depth explanation can be found here: Why I have to call 'exit' after redirection through header('Location..') in PHP?

EDIT

It's clear now that Milen Georgiev answer is correct. You output content to the browser before you reach the header(); You need to move the if statement in the top part of your PHP code to avoid the header content error.

if (isset ( $_GET ['reserved'] )) { 
    $sqlreserve = "INSERT INTO calendar (eventDate, timeslot) VALUES ('" . $dateToCompare . "','" . intval($_GET['t']) . "');";
    $resultreserve = mysqli_query ( $mysqli1, $sqlreserve );
    if ($resultreserve) {
        header('Location: '.$_SERVER['PHP_SELF']);
        exit;
    } else {
        echo "Event Failed to add";
    }
}

Do you send(output/echo) anything before header('Location: '.$_SERVER['PHP_SELF']); ?

If that is the case, you will have to turn the object buffer on. Using header, you always have to send the headers first or it will not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM