Languages

Test Page

 

document.getElementById("nameForm").addEventListener("submit", function(event) { event.preventDefault(); let name = document.getElementById("nameInput").value; // Call a PHP script using AJAX to check the name against the CSV file // Update the result div based on the response }); $csvFile = 'names.csv';

// Check if the CSV file exists, and create it if it doesn't
if (!file_exists($csvFile)) {
$fp = fopen($csvFile, 'w');
fclose($fp);
}

// Read the CSV file
$csv = array_map('str_getcsv', file($csvFile));

// Check if the submitted name is in the CSV file
$newName = $_POST['name'];
$nameExists = false;
foreach ($csv as $row) {
if ($row[0] == $newName) {
$nameExists = true;
break;
}
}

// Add the name if it doesn't exist
if (!$nameExists) {
$fp = fopen($csvFile, 'a');
fputcsv($fp, array($newName));
fclose($fp);
}

// Return the result
echo json_encode(array("nameExists" => $nameExists, "newName" => $newName));
?>