Dear all,
I am try to create a dynamic pull-down menu which queries a MYSQL database for the states of Malaysia. I have been successful with this and the code is as follows:
<?php
$query = mysql_query(
"SELECT s_id,
s_name
FROM state
ORDER BY s_name ASC"
);
$servicepulldown = '<option>Select</option>';
while ($data = mysql_fetch_array($query, MYSQL_ASSOC))
{
echo " <option value='{$data['s_id']}'>{$data['s_name']}, </option>\n";
}
I can't figure out how to keep the data selected if a different part of the form is incorrectly filled out. This is the code I have been using but it doesn't work. I get a "Undefined index: state" error.
$query = mysql_query(
"SELECT *
FROM state ORDER BY s_name ASC"
);
$statepulldown = '<option>Select</option>';
while ($data = mysql_fetch_array($query, MYSQL_ASSOC)) {
if (($_POST['state']) == $data['s_name']) {
$statepulldown .= "<option value=\"{$_POST['state']}\" selected=\"selected\">{$data['s_name']}</option>\n";
} else {
$statepulldown .= "<option value='{$data['s_id']}'>{$data['s_name']}, </option>\n";
}
}
I'd really appreciate any suggestions. I'm new to PHP and this problem has been driving me insane.
Kind regards,
Luke