php customer form with input and save to a file

Here's a basic PHP code to create a customer form with input fields and save the data to a file:

HTML Form Code:

<form method="post" action="save_data.php">
  <label for="name">Name:</label>
  <input type="text" name="name" id="name" required>
  <label for="email">Email:</label>
  <input type="email" name="email" id="email" required>
  <label for="phone">Phone:</label>
  <input type="text" name="phone" id="phone" required>
  <input type="submit" value="Submit">

PHP Code to Save Data to a File (save_data.php):

// Get form data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];

// Define file name and location
$file = "customer_data.txt";

// Open file in append mode
$fp = fopen($file, "a");

// Write customer data to file
fwrite($fp, $name . "," . $email . "," . $phone . "\n");

// Close file

// Redirect user back to form
header("Location: customer_form.php");

This code will save the customer data to a file called "customer_data.txt" in the same directory as the PHP script. The data will be saved in CSV format, with each line containing the name, email, and phone number of a single customer.

Note: Be sure to validate and sanitize the user input to prevent security issues like SQL injection or cross-site scripting (XSS).

Here's an example PHP code that reads the customer data from the "customer_data.txt" file and displays it in a table format using your custom layout:

// Define file name and location
$file = "customer_data.txt";

// Read file into array
$data = file($file);

// Output table header
echo "<table>";
echo "<tr><th>Name</th><th>Email</th><th>Phone</th></tr>";

// Loop through data and output table rows
foreach($data as $line) {
  $fields = explode(",", $line);
  echo "<tr>";
  echo "<td>" . $fields[0] . "</td>";
  echo "<td>" . $fields[1] . "</td>";
  echo "<td>" . $fields[2] . "</td>";
  echo "</tr>";

// Output table footer
echo "</table>";

In this code, we first define the file name and location, and then use the file() function to read the contents of the file into an array.

We then loop through the array and split each line into fields using the explode() function. We output the data in a table format using your custom layout, with each row containing the name, email, and phone number of a single customer.

You can customize the HTML code inside the loop to match your custom layout.

Related Articles

- All From ChatGPT