Howto pass a textfield value to a pop form? I need to pass the value so
that the drop down box on the form would autopopulate the items related
to that value in the textfield. I'm using PHP. any help please?

Dani AI

Generated

Following ’s note that JavaScript is required and ’s preference for faux-popups, here are two practical, safe ways to pass a textfield value into a pop form and populate a dropdown — a preferred in-page modal (no popup blockers) and a simple real-popup fallback.

Recommended (modal + server lookup): keep the form markup hidden on the same page and load options from a PHP endpoint as JSON. This avoids popup blockers, keeps focus management simple, and lets the server sanitize queries. Minimal example:

/* when opening the pop form — fetch options and show modal */
document.querySelector('#openBtn').addEventListener('click', async () => {
  const q = document.querySelector('#sourceField').value.trim();
  if (!q) return;
  const res = await fetch('/get-options.php?q=' + encodeURIComponent(q));
  const items = await res.json(); // [{id:'1',label:'One'},...]
  const sel = document.querySelector('#popupSelect');
  sel.innerHTML = '';
  items.forEach(i => {
    const opt = document.createElement('option');
    opt.value = i.id;
    opt.textContent = i.label;
    sel.appendChild(opt);
  });
  document.querySelector('#modal').style.display = 'block';
});

Server-side (PHP) — return clean JSON and use prepared statements:

<?php
header('Content-Type: application/json; charset=utf-8');
$q = trim($_GET['q'] ?? '');
if ($q === '') { echo json_encode([]); exit; }
// $pdo = new PDO(...); use credentials and error handling
$stmt = $pdo->prepare('SELECT id, name AS label FROM items WHERE name LIKE ? LIMIT 50');
stmt->execute(["%$q%"]);
echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));

Fallback (real popup): pass the value in a query string or use postMessage for cross-window comms. Example opener:

const val = document.querySelector('#sourceField').value;
window.open('popup.php?val=' + encodeURIComponent(val), 'popupWin', 'width=500,height=400');

Then read $_GET['val'] in popup.php to pre-select options. Notes and cautions: always sanitize server inputs, escape output to prevent XSS, prefer the modal pattern for accessibility and to avoid blockers, use window.postMessage when communicating across origins, and ensure keyboard/focus handling so the popup/modal is usable for keyboard and screen-reader users. This expands on @ardav’s AJAX suggestion with a concrete, secure workflow.

Recommended Answers

All 3 Replies

You would need to use javascript

Member Avatar for Member #120589

I sincerely hope that you use faux-popups like lightboxes as opposed to new window popups.

Couple of methods open to you:

1) Place static arrays of values into JS - when a dropdown option chosen onselect changes the values in the popup via innerHTML value.
2) Use ajax to retrieve data on the server (usually from a DB - but could be from hard-coded variables) and change values as above.

Hi meffe and ardav,

So I would need java script to do this.

Thank you for the quick response.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.