WENO eRx Module: Drug Field Breaks Free-Text Entry (Missing AJAX File)
OpenEMR Version: 7.0.4
Module: WENO eRx (oe-module-weno)
Category: Bug Report / Fix
Problem Description
After installing and configuring the WENO eRx module, the Drug field on the standard Add Prescription popup stops accepting free-text entries. Prior to WENO installation, providers could type any text into the Drug field (e.g., “Lab Testing,” “Walker,” “Wheelchair”) and it would be accepted. After WENO installation, typing in the field produces a gray message: “The results could not be loaded.” The entry is not saved.
Root Cause
The WENO module modifies templates/prescription/general_edit.html.twig to replace the standard Select2 drug lookup with a new AJAX-based autocomplete when weno_rx_enable is true:
$("#drug").select2({
ajax: {
url: "library/ajax/drug_autocomplete/search.php",
...
}
});
This calls /library/ajax/drug_autocomplete/search.php — a file that does not exist in OpenEMR. Every keystroke in the Drug field returns an HTTP 404, causing Select2 to display “The results could not be loaded.”
A secondary bug also exists in the same block: the processResults function uses id: index (an integer) instead of id: item (the drug name string), which causes the selected value to not be properly registered for form submission — resulting in a “Missing a required field” error on save even when a selection appears to be made.
Fix
Step 1 — Create the missing AJAX endpoint
Create the directory and file:
/var/www/html/openemr/library/ajax/drug_autocomplete/search.php
File contents:
<?php
require_once dirname(__FILE__, 4) . '/interface/globals.php';
use OpenEMR\Common\Csrf\CsrfUtils;
if (!CsrfUtils::verifyCsrfToken($_GET['csrf_token_form'] ?? '')) {
http_response_code(403);
exit;
}
$term = trim($_GET['term'] ?? '');
if ($term === '') {
header('Content-Type: application/json');
echo json_encode([]);
exit;
}
// Try NIH RxNav API for real drug names
$url = "https://rxnav.nlm.nih.gov/REST/Prescribe/drugs?name=" . urlencode($term);
$response = @file_get_contents($url);
$results = [];
if ($response) {
preg_match_all('/<name>([^<]+)<\/name>/', $response, $matches);
if (!empty($matches[1])) {
foreach ($matches[1] as $name) {
$results[] = $name;
}
}
}
// Always include the typed text as a free-text option (first in list)
array_unshift($results, $term);
header('Content-Type: application/json');
echo json_encode($results);
Set correct ownership:
sudo chown www-data:www-data /var/www/html/openemr/library/ajax/drug_autocomplete/search.php
Step 2 — Fix the id field in processResults
In templates/prescription/general_edit.html.twig, inside the {% if weno_rx_enable %} block, change:
return {
text: item,
id: index,
value: item
}
To:
return {
text: item,
id: item,
value: item
}
Result
After both fixes:
- Real drug names are looked up via the NIH RxNav API as expected
- Free-text entries (e.g., “Lab Testing,” “Walker,” “Wheelchair”) are always presented as the first dropdown option and accepted normally
- The selected value is properly registered for form submission
Notes
- This issue affects any practice that writes non-drug prescriptions (lab orders, DME equipment, etc.) using the standard OpenEMR prescription form
- The fix is backward compatible — real drug lookups continue to work normally
- Tested on OpenEMR 7.0.4 with the WENO eRx module installed
Reported by: Jeff Guillory, DNP, APRN, FNP-BC
NP Health Clinic, PLLC — Lumberton, TX