tobiasvestlund tobiasvestlund - 1 month ago 7
HTML Question

Populate dropdown with Microsoft Access values - HTML & PHP

I'm doing my best trying to populate a dropdown from my access db using php and html.

I have a table in my DATA.mdb that I've named CONTACTS and it contains 4 columns and some rows, as below:



Id    Person    Phone    Email    




10    Pers1     112         mail1    




10    Pers2     113         mail2    




10    Pers3     114         mail3    




20    Pers4     211         mail4    




20    Pers5     311         mail5    




When I enter 10 to my input="text" I get an echo that looks like this:
Pers1Pers2Pers3

and when I enter 20 I get
Pers4Pers5

This is what i've manage to do with the PHP:

<?php

if (isset($_POST['n01_text']))
{
ob_start();
$stxt = rtrim($_POST['n01_text']);

$keys = preg_split("/[\s,]+/", $stxt);

//- Open DATA
$db = odbc_connect('DATA','','') or die(odbc_errormsg());

//- Read CONTACTS
$sql = "SELECT ID, Person, Phone, Email FROM CONTACTS WHERE ID= '" . $ID. "'";
$rs = odbc_exec($db,$sql) or die(odbc_errormsg());

while (odbc_fetch_row($rs)) {
$Person= odbc_result($rs,"Person");
$Phone= odbc_result($rs,"Phone");
$Email = odbc_result($rs,"Email ");

echo $Person;
}

odbc_close($db);
ob_end_flush();
}
?>


And here is the HTML:

<input type="text"name="n01_text" placeholder="ID" class="findtext" id="contact"> <span id="myspan" class="input-group-addon">Search</span>

<select>
<option value=0><?php echo $Person; ?></option>
<!-- Populate from Access-->
</select>


From this code, I only get the last entry from the db to my dropdown, which is Pers3 for ID 10 and Pers5 for ID 20.

I really don't have any clue of how to solve this.

I am trying my best to learn and I am sure there a tons of way my code can improve and I appreciate all kinds of corrections, but please be patient with me!

Answer

do this:

$Person .= "<option value=$i>".odbc_result($rs,"Person")."</option>";
$i++;

and then update the HTML to:

<input type="text"name="n01_text" placeholder="ID" class="findtext" id="contact"> <span id="myspan" class="input-group-addon">Search</span>

<select>
<?php echo $Person; ?>
<!-- Populate from Access-->
</select>