$_POST returning empty values

I have created a form, as shown below that displays according to a switch statement. I am needing the inputs from the form submission to either be saved to a table in the WordPress Database or to be saved to a text file saved in the uploads section of the sites core files. However, when the form is submitted it is outputting empty values for the variables. What am I doing wrong? How can this be fixed? Thank you!

Code for the form’s switch statement:

<?php /* code for pop-up button conditions */
$host = 'https://' .$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; /* gets the current page's URL */
switch ( $host ){
  case 'https://example1.com/': 
  case 'https://example2.com/': 
  case 'https://example3.com/':
    require 'pop-up-button.php'; /*calls the PHP file containing the code for the form*/
    echo '<button id="close" class="open-button" onclick="openForm()"></button>'; /*echo runs and outputs the HTML code for the button */
    break; 
  default : 
    break; 
}; ?>

Code for the form:

<html>
<style>

/* Stylings for Heading Statement */
    .h3{
        text-align: center;
        font-size: 24px;
        padding: 10px;
        color: white;
    }
/* Stylings for checkbox */
    .box{
        font-size: 20px;
        padding: 20px;
        color: white;
    }
/* Stylings for input labels (names and checkbox labels) */
    .input-label{
        text-align: center;
        font-size: 20px;
        padding: 10px;
        color: white;
    }
/* Button used to open the contact form - fixed at the bottom of the page */
    .open-button {
        width: 170px; 
        height: 60px; 
        background-color: transparent; 
        outline: none; 
        position: relative; 
        left: -168px; 
        top: 20px;
    }
/* The popup form - hidden by default */
    .form-popup {
        display: none;
        position: absolute;
        top: 25%;
        border: none;
        z-index: 9;
        background-color: #1e1e1e;
      width: 100%;
        padding: 20px;
        border-radius: 20px;
        text-align: center;
    }
/* Sets style and size of input fields  */
    .form-popup input[type=text] {
        width: 20%;
        padding: 5px;
        margin: 5px 0 22px 0;
        border: none;
        background: #FFFFFF;
    }
/* Sets color and style of the input boxes when selected */
    .form-popup input:focus {
        background-color: #FFFFFF;
        outline: none;
    }
/* Stylings of the submit button */
    .btn {
        background-color: #DB2600;
        border-radius: 9999px;
        font-size: 24px;
        width: 120px;
        height: 45px;
        transition: 0.3s;
        color: white;
    }
/* Changes the color of the sugmit button when curser hovers over it */
    .btn:hover {
        opacity: 1;
        background-color: #000000;
    }
/* Hides the default checkbox */
    .box input {
        position: absolute;
        opacity: 0;
        cursor: pointer;
        height: 0;
        width: 0;
    }
/* creates custom checkbox */
    .checkmark {
        position: absolute;
        height: 20px;
        width: 20px;
        background-color: white;
        border-radius: 2px;
    }
/* Sets the color of checkbox when checked */
    .box input:checked ~ .checkmark {
        background-color: #DB2600;
    }
/* creates checkmark inside checkbox */
    .checkmark:after {
        content: "";
        position: absolute;
        display: none;
    }
/* Displays checkmark when checkbox is clicked */
    .box input:checked ~ .checkmark:after {
        display: block;
    }
/* Positions and styles checkmark */
    .box .checkmark:after {
        left: 7px;
        top: 3px;
        width: 6px;
        height: 12px;
        border: solid white;
        border-width: 0 3px 3px 0;
        -webkit-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        transform: rotate(45deg);
    }
/* Column formatting for checkbox and statement. This is what is aligning the two items. */
    .column1 {
        float: left;
        width: 17%;
    }
    .column2 {
        float: left;
        width: 74%;
        margin-top:-5px;
    }
/* Clears floats after the columns */
    .row:after {
        content: "";
        display: table;
        clear: both;
    }

</style>
<body>

<!-- Code for the form -->

    <div class="form-popup" id="Form"> 
        <form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
        
        <!-- acknolwdgement statment -->
              <h3 class="h3">Form Statement</h3> 

        <!-- label and input for First Name -->
              <label for="fname" class="input-label">First name:</label>
              <input type="text" name="fname" id="fname" required/> 

        <!-- label and input for Last Name -->
              <label for="lname" class="input-label">&nbsp &nbsp Last name:</label>
              <input type="text" name="lname" id="lname" required/> <br> <br>

        <!-- checkbox and label, placed to columns to achieve desired formating -->
              <div class="row">
                  <div class="column1" style="text-align: right;">
                      <label  class="box" style="border-color: red;">  
                      <input type="checkbox" name="consent" value="agreed" class="box" id="consent" required/>
                      <span class="checkmark"></span>
                      </label></div>
                  <div class="column2" style="text-align: left;">
                      <label class="input-label"> I have read and understand the above statement.
                      </label>
                  </div>
              </div>
              <br><br>  
        <!-- Submit Button -->
              <input type="submit" onclick = "submitForm()" class="btn">
            

        </form>
    </div>

<script>
    function openForm() { /* Opens Consent Form */
        document.getElementById("Form").style.display = "block";
    }
    function submitForm() {
        document.write = "<?php form_process() ?>"; 
    }
</script>

</body>

</html>

PHP that processes the form: (contains statements for both Database insertions and writing to the text file, I truly only need one to work)

<?php /* PHP code that processes form */

    function form_process(){
       
        $FirstName = filter_input(INPUT_POST, "fname", FILTER_SANITIZE_STRING);
        $LastName = filter_input(INPUT_POST, "lname", FILTER_SANITIZE_STRING);
        $Consent = filter_input(INPUT_POST, "consent", FILTER_SANITIZE_STRING);

        global $wpdb;
        $mysqli = new mysqli("host", "usernme", "password", "database");
        $table_name = $wpdb -> database . 'table';
        $wpdb->insert($table_name, array('FirstName' => "$FirstName", 'LastName' => "$LastName", 'Consent' => "$Consent"));

        $Date = date("l, F jS, Y");
        date_default_timezone_set("America/Chicago");
        $Time = date("h:i:sa");

        $data = 'I, ' . $FirstName . ' ' . $LastName . ', ' . $Consent . ' to Form on ' . $Date . ' at ' . $Time . '.' . "n"; 
        $txtfile = "uploads/Form.txt";
        $fp = fopen($txtfile, 'a+');
        fwrite($fp, $data);
        fclose($fp);
    }
?>

Database output:

Database

Text file output:

Text File

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

$_POST returning empty values

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.