I've searched all over and can't find the right answer for this today. It seems like a pretty ordinary need.
I'm working on a page in php that has a 3 mutual radio buttons. When you click one of them I need to populate a select box with a different set of data from an MS SQL Server database.
I know how to get the data from the database. I know how to set up the form items. I've tried jquery, ordinary javascript, other random ideas. Nothing I'm doing works or even makes sense. Yes I know the difference between server side and client side.
The list of counties is slightly different depending on which radio button the user chooses. Obviously I don't want to post the form at this point. The user is supposed to fill in some other options including make a selection of the counties from the multiselect box.
The radio buttons and the select are on the same form with some other options like choosing dates that the user has to fill in before hitting submit.
I'm totally stuck. All my online searching comes up with solutions to other problems that are slightly related but not quite there. I know how to change the values of a select box in javascript based on which radio button is clicked, but I have to get the counties from the database, not hard code them into the php file.
I'm thinking maybe the php code that gets the data has to come from a separate php file and it's called using ajax but I'm not sure I'm on the right track.
What I have tried:
So I've got my 3 radio buttons:
<td>
<input type="radio" name="in_out" id="in_out" value="I"> In County<br>
<input type="radio" name="in_out" id="in_out" value="O" checked> Out County<br>
<input type="radio" name="in_out" id="in_out" value="B"> Both<br>
</td>
Here's my php code to populate the select box:
<select name="county_list" multiple size="10" style="width:120px" >
<?php
$in_out = 'B';
if ($in_out == 'I') {
$db_query = "SELECT County FROM dbo.tblCounties WHERE InOut = 'I' AND DummyCounty = 0 ORDER BY County DESC";
} else if ($in_out == 'O') {
$db_query = "SELECT County FROM dbo.tblCounties WHERE InOut = 'O' AND DummyCounty = 0 ORDER BY County DESC";
} else {
$db_query = "SELECT County FROM dbo.tblCounties WHERE DummyCounty = 0 ORDER BY County DESC";
}
try {
$stmt = $pdo->prepare($db_query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST);
do {
$county = $row[0];
echo '<option value="' . $county . '">' . $county . '</option>';
} while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_PRIOR));
$stmt = null;
} catch (PDOException $e) {
print $e->getMessage();
}
?>
</select>