Added code to get comments from database

This commit is contained in:
Peter Edmond 2025-06-02 17:24:32 +01:00
parent 827924bd26
commit 5dc5d0c728
4 changed files with 165 additions and 1509 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

58
get_text_data.html Normal file
View File

@ -0,0 +1,58 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Form Submit Example</title>
<style>
textarea {
width: 100%;
height: 200px;
margin-top: 1em;
}
</style>
</head>
<body>
<form id="qidForm">
<label for="qidInput">Enter QID:</label>
<input type="text" id="qidInput" name="qid" required /><br />
<label for="survey">Survey: (SV_cwKjMqAqGxImjMG)</label>
<input type="text" id="survey" name="survey" required /><br />
<label for="Q1">Enter Q1:</label>
<input type="text" id="Q1" name="Q1" /><br />
<label for="Q2">Enter Q2:</label>
<input type="text" id="Q2" name="Q2" /><br />
<label for="Q3">Enter Q3:</label>
<input type="text" id="Q3" name="Q3" /><br />
<button type="submit">Get Counts</button>
</form>
<textarea id="output" readonly placeholder="Results will appear here..."></textarea>
<script>
document.getElementById('qidForm').addEventListener('submit', async function (e) {
e.preventDefault();
const formData = new FormData(this);
try {
const response = await fetch('get_text_data.php', {
method: 'POST',
body: formData
});
const text = await response.text();
document.getElementById('output').value = text;
} catch (error) {
document.getElementById('output').value = 'Error: ' + error.message;
}
});
</script>
</body>
</html>

107
get_text_data.php Normal file
View File

@ -0,0 +1,107 @@
<?php
header('Content-Type: application/json');
if (php_sapi_name() === 'cli') {
echo "Running from command line.\n";
$qid="QID64_TEXT";
$survey="SV_cwKjMqAqGxImjMG";
$q1 = 1;
$q2 = 2;
$q3 = 0;
} else {
//echo "Running from browser.\n";
// Basic input validation and sanitization
if (!isset($_POST['qid']) || empty($_POST['qid'])) {
echo json_encode(['error' => 'Missing QID parameter']);
exit;
}
$qid = $_POST['qid'];
$input = $_POST['survey'] ?? '';
if (preg_match('/^SV_[a-zA-Z0-9]+$/', $input)) {
// Input is valid
$survey = $input;
} else {
// Invalid format
die("Invalid survey ID format.");
}
$q1 = filter_input(INPUT_POST, 'Q1', FILTER_VALIDATE_INT);
$q2 = filter_input(INPUT_POST, 'Q2', FILTER_VALIDATE_INT);
$q3 = filter_input(INPUT_POST, 'Q3', FILTER_VALIDATE_INT);
}
$qualifier = " AND s.surveyId = '${survey}'";
if ($q1 > 0) {
$qualifier.=" AND r.Q1 = " . $q1;
}
if ($q2 > 0) {
$qualifier.=" AND r.Q2 = " . $q2;
}
if ($q3 > 0) {
$qualifier.=" AND r.Q3 = " . $q3;
}
// Database connection (adjust credentials accordingly)
//
$config = require 'config.php';
$host = $config['db_host'];
$db = $config['db_name'];
$user = $config['db_user'];
$pass = $config['db_pass'];
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
$sql = "
SELECT
a.text
FROM Answers a
INNER JOIN Responses r ON a.responseId = r.id
INNER JOIN Surveys s ON a.surveyId = s.id
WHERE a.QID = :qid
".$qualifier;
// echo $sql;
$stmt = $pdo->prepare($sql);
$stmt->execute(['qid' => $qid]);
$results = $stmt->fetchAll();
$json = json_encode($results);
echo $json;
if ($json === false) {
echo json_last_error_msg();
}
} catch (PDOException $e) {
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
exit;
}