export – Exporting a form to a CSV file does not work in WordPress but it does in PHP

Question

I have a form with some fields that I need to implement in a WordPress template. If I do it in vanilla PHP it works but when I put it in a WordPress template it doesn’t work, can it be a problem with $_POST?

<?php
/**
 * Template Name: Telesales
 */
?>

<?php
if(isset($_POST['submit'])){

    
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $DOFMonth = $_POST['DOFMonth'];
    $DOFDay = $_POST['DOFDay'];
    $DOFYear = $_POST['DOFYear'];

    
    if(!isset($error)){

        $Content = "Name,LastName,Birthday\n";
        $Content .= "$firstName,$lastName,$DOFMonth-$DOFDay-$DOFYear\n";

        $FileName = $firstName."-".date("d-m-y-h:i:s").".csv";
        header('Content-Type: application/csv'); 
        header('Content-Disposition: attachment; filename="' . $FileName . '"'); 
        echo $Content;
        exit();
    }
}


if(isset($error)){
    foreach($error as $error){
        echo '<p style="color:#ff0000">$error</p>';
    }
}
?>

<form action="" method="post">

  <label>First name</label>
  <input type="text" name="firstName" placeholder="Rose" required="" value="">
  <br>

  <label>Last name</label>
  <input type="text" name="lastName" placeholder="McDonnell" required="" value="">
  <br>

  <label>Date of Birth</label>
  <select name="DOFMonth" required="">
    <option value="null">Month</option>
    <option value="January">January</option>
    <option value="February">February</option>
  </select>

  <select name="DOFDay" required="">
    <option value="null">Day</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
  </select>
  <select name="DOFYear" required="">
    <option value="null">Year</option>
    <option value="1920">1920</option>

  </select>


  <input type="submit" name="submit" value="Submit">

</form>

0
Tom J Nowell 6 months 2022-02-21T05:57:46-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse