<select name='brgylist' id='list'>
                    <?php
                    require_once "connection.php";
                    $result = mysql_query("SELECT `BrgyCode`,`Barangay` FROM `ref Barangay`");
                    while($rec = mysql_fetch_array($result))
                    {
                        echo '<option value="'.$rec['BrgyCode'].'">'.$rec['Barangay'].'</option>';

                    }
                    echo 'The Code selected is '.$rec['BrgyCode'];
                    ?>

                    </select>

I want to store the selected dropdown list to a variable because i will use it to query to output in different dropdownlist. please i need your help on this.

Dani AI

Generated

Two common, reliable ways to use the selected value from one <select> to populate another are: (A) submit the form and let PHP read the selected value (server-side rebuild), or (B) make the page dynamic with JavaScript + an AJAX call to a small PHP endpoint that returns the dependent options as JSON. Approach B gives a smoother UX and avoids full-page reloads.

For example workflow (AJAX):

  1. Give the first select an id. 2) On its change event, send its value to a PHP endpoint. 3) The endpoint queries using a prepared statement and returns JSON. 4) Replace the options in the second select using safe DOM methods.

JavaScript (client side)

document.getElementById('brgySelect').addEventListener('change', function() {
  const code = this.value;
  const dest = document.getElementById('streetSelect');
  dest.disabled = true;
  dest.innerHTML = '<option>Loading...</option>';

  fetch('get_streets.php', {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({ brgy: code })
  })
  .then(r => r.ok ? r.json() : Promise.reject('Network error'))
  .then(rows => {
    dest.innerHTML = '';
    rows.forEach(r => {
      const o = document.createElement('option');
      o.value = r.id;
      o.textContent = r.name;
      dest.appendChild(o);
    });
    dest.disabled = false;
  })
  .catch(() => {
    dest.innerHTML = '<option>Error loading</option>';
  });
});

PHP endpoint (server side) — use PDO or mysqli with prepared statements, return JSON:

<?php
// get_streets.php (sketch)
require 'connection.php'; // provide $pdo
$in = json_decode(file_get_contents('php://input'), true);
$brgy = $in['brgy'] ?? '';
$stmt = $pdo->prepare('SELECT id,name FROM streets WHERE brgy_code = ?');
$stmt->execute([$brgy]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
header('Content-Type: application/json; charset=utf-8');
echo json_encode($rows);

Notes and troubleshooting:

  • Do not use the old mysql extension; use PDO/mysqli and prepared statements to avoid SQL injection.
  • When inserting option text, use textContent or htmlspecialchars to prevent XSS.
  • Check the browser console and Network tab if nothing appears; verify the endpoint returns valid JSON and correct Content-Type.
  • : the HTML selected attribute preselects an option — it does not capture a runtime user selection for use by another query. asked the right follow-up: decide whether you want a full submit or dynamic cascading selects. : avoid echoing loop variables after the loop to get the chosen value — read it from $_POST on submit or from the client-side value in JS.

Recommended Answers

All 2 Replies

Use the "selected" key word as

<select name='brgylist' id='list'>
<?php
require_once "connection.php";
$result = mysql_query("SELECT `BrgyCode`,`Barangay` FROM `ref Barangay`");
while($rec = mysql_fetch_array($result))
{
echo '<option value="'.$rec['BrgyCode'].'" selected="selected">'.$rec['Barangay'].'</option>';
}

?>
</select>

Are you asking how you can use the item that the user selects in your <select> to fill another <select> element?

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.