如何避免OnePager网站上的页面刷新上的数据重新提交+避免浏览器弹出窗口

如何避免OnePager网站上的页面刷新上的数据重新提交+避免浏览器弹出窗口

问题描述:

i already searched a lot but none of these solutions can help me. I want to avoid Data Re-Submit on Page Refresh on a Onepager Site. I don´t got any other page to redirect. + Avoid Browser PopUp

I am a total beginner so sorry for the "terrible code".

<?php
$errors = array();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $issend = true;
    if (isset($_POST['name'])) {
        $name = $_POST['name'];
    }

    if (isset($_POST['phonenumber'])) {
        $phonenumber = $_POST['phonenumber'];
    }

    if ($phonenumber === null || mb_strlen($phonenumber) === 0) {
        $errors[] = 'phonenumber.empty';
    }

    if ($name === null || mb_strlen($name) === 0) {
        $errors[] = 'name.empty';
    }


    if (empty($errors)) {
        mail('email@email.com', 'Contact Form', 'Name: '.$_POST["name"] . '
 Telefonnummer:' .$_POST["phonenumber"]);
        $_POST['name'] = '' ;
        $_POST['phonenumber'] = '' ;
        $_POST = null;
    }

}

on a successful submission, redirect the user to a success page. If you don't have one, just create a simple HTML/PHP page

success.php

<?php
    echo "Successful submission";
?>

Your page with the form add this to bottom of your check

if (empty($errors)) {
        mail('email@email.com', 'Contact Form', 'Name: '.$_POST["name"] . '
 
        Telefonnummer:' .$_POST["phonenumber"]);
        $_POST['name'] = '' ;
        $_POST['phonenumber'] = '' ;
        $_POST = null;

        header('Location: http://www.domainname.com/success.php');
    }