HTML form to csv file on server

Joined
Feb 12, 2025
Messages
2
Reaction score
0
Afternoon all,

I've been searching online for an answer to this.

Requirement:
I want to have a webpage with a form on it.
The data from this form once the submit button is pressed it populates a csv (data.csv) on the server with the website is hosted.

The webpage with the form on it will be called: input.php
I have found this code.

HTML:
<!DOCTYPE html>

<html lang="en">

  <head>

    <meta charset="UTF-8" />

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title>Document</title>

    <link rel="stylesheet" href="./css/main.css" />

    <script src="./js/app.js"></script>

  </head>

  <body>

<form name="xyz_form" action="process.php" method="get">

    <section class="border-bottom">

<div class="content">

    <h3>ID Number</h3>

    <div class="form-control-group">

        <div class="form-control form-control-number">

            <input type="number" id="id_number">

        </div>

    </div>

            <h3>First Name</h3>

    <div class="form-control-group">

        <div class="form-control form-control-text">

            <input type="text" id="first_name">

        </div>

    </div>

</div><!--.content-->

    <section class="data-capture-buttons one-buttons">

       <div class="content">

          <input type="submit" value="Submit" onClick="javascript:addToCSVFile()">

       </div>

    </section><!--.data-capture-buttons-->

    

  </body>

</html>

The php page will be called process.php:

PHP:
<?php



    $keys = array('id_number','first_name');



    $csv_line = array();



    foreach($keys as $key){



        array_push($csv_line,'' . $_GET[$key]);



    }



    $fname = 'data.csv';



    $csv_line = implode(',',$csv_line);



    if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line;}



    $fcon = fopen($fname,'a');



    $fcontent = $csv_line;



    fwrite($fcon,$csv_line);



    fclose($fcon);



// echo file_get_contents($fname);



?>

the csv has only two columns
id_number
first_name

When the Submit button is pressed it goes to the process page with these errors.

Warning: Undefined array key "id_number" in C:\xampp\htdocs\testsite\process.php on line 5

Warning: Undefined array key "first_name" in C:\xampp\htdocs\testsite\process.php on line 5

But still nothing is written to the data.csv



Could someone please help.

Thank you in advance,
 
Joined
Feb 12, 2025
Messages
2
Reaction score
0
I may have found a solution:

It does put the date in the csv in a bit of a different way.

HTML:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Export to CSV</title>
</head>
<body>
    <form action="export.php" method="post">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name" required><br>
 
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required><br>
 
        <input type="submit" value="Submit">
    </form>
</body>
</html>

PHP:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Collect form data
    $name = $_POST['name'];
    $email = $_POST['email'];
 
    // Create a CSV file
    $filename = "data.csv";
    $file = fopen($filename, "a"); // Open the file in append mode
 
    // Prepare the data to be written
    $data = [$name, $email];
 
    // Write the data to the CSV file
    fputcsv($file, $data);
 
    // Close the file
    fclose($file);
 
    // Provide feedback to the user
    echo "Data exported successfully to $filename.";
} else {
    echo "No data submitted.";
}
?>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,239
Messages
2,571,200
Members
47,836
Latest member
Stuart66

Latest Threads

Top