forked from Anishkagupta04/RAPIDOC-HEALTHCARE-WEBSITE-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
medicine.js
35 lines (30 loc) · 1.64 KB
/
medicine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
document.getElementById('medicine-form').addEventListener('submit', function(event) {
event.preventDefault();
const medicineName = document.getElementById('medicine-name').value;
fetchMedicineInformation(medicineName);
});
async function fetchMedicineInformation(medicineName) {
try {
const response = await fetch(`https://api.fda.gov/drug/label.json?search=openfda.brand_name:${medicineName}`);
const data = await response.json();
displayMedicineInformation(data);
} catch (error) {
console.error('Error fetching medicine information:', error);
document.getElementById('medicine-info').innerHTML = '<p>There was an error fetching the medicine information. Please try again later.</p>';
}
}
function displayMedicineInformation(data) {
const medicineInfoSection = document.getElementById('medicine-info');
medicineInfoSection.innerHTML = '';
if (data.results && data.results.length > 0) {
const medicineInfo = data.results[0];
medicineInfoSection.innerHTML = `
<h2>${medicineInfo.openfda.brand_name ? medicineInfo.openfda.brand_name[0] : 'No brand name available'}</h2>
<p><strong>Purpose:</strong> ${medicineInfo.purpose ? medicineInfo.purpose.join(', ') : 'No purpose information available'}</p>
<p><strong>Warnings:</strong> ${medicineInfo.warnings ? medicineInfo.warnings.join('<br>') : 'No warnings available'}</p>
<p><strong>Directions:</strong> ${medicineInfo.directions ? medicineInfo.directions.join('<br>') : 'No directions available'}</p>
`;
} else {
medicineInfoSection.innerHTML = '<p>No information found for the specified medicine.</p>';
}
}