In my PHP generated webpage there are 3 linked listboxes. When the user changes the selection in list_1, the contents of list_2 and list_3 changes accordingly (e.g. as it would change in 3 lists containing continent -> country -> city
).
The code is:
PHP (032A_GetProvincie.php) [EDITED]
$provincie = array();
$query = 'SELECT nume_prov FROM sa_Provincie ';
$query .= 'WHERE provincie_id > 777 ';
$query .= 'ORDER BY nume_prov';
// Get query ---------------------------------------------------
$query_data = mysqli_query($db_conn, $query);
if ($query_data == FALSE)
{
array_push($provincie, 'None found');
goto _adr1;
}
$rows = mysqli_num_rows($query_data);
if ($rows == 0)
{
array_push($provincie, 'None found');
goto _adr1;
}
// Build array -------------------------------------------------
array_push($provincie, 'All_';
while ($row = mysqli_fetch_array($query_data))
array_push($provincie, $row['nume_prov']);
// End
_adr1:
print json_encode($provincie);
mysqli_free_result($query_data);
mysqli_close($db_conn);
Javascript
$(document).ready(function()
{
$.getJSON("/scripts/032A_GetProvincie.php", function(data)
{
var str_options = "";
for (var i = 0; i < data.length; i++)
{
str_options += "<option value='" + data[i] + "'>" + data[i] + "</option>
";
}
$("#listaProvincie").append(str_options);
$("#listaProvincie").change();
});
$("#listaProvincie").change(function()
{
$.getJSON("/scripts/032B_GetRegiune.php?provincie=" + $(this).val(), function(data)
{
var str_options = "";
for (var i = 0; i < data.length; i++)
{
str_options += "<option value='" + data[i] + "'>" + data[i] + "</option>
";
}
$("#listaRegiune").html("");
$("#listaRegiune").append(str_options);
$("#listaRegiune").change();
});
});
...
listaProvincie is list_1, listaRegiune is list_2, etc. When data is returned by the PHP queries, the lists are populated accordingly;
however, if the query there returns no data, the line 'None found' does not appear as an option in list_1, the list remains BLANK.
I am a Javascript rookie, so maybe I do not understand something elementary, but what is wrong with my code ? And please don't try to give me a solution that uses Ajax, I know almost nothing about it.
Note: In the code I show here, the WHERE clause of my PHP query deliberately contains a condition provincie_id > 777
that I know returns no rows, yet list_1 remains blank. I am thinking, any value I push in the array should appear in the list, shouldn't it ?
[EDIT 1] No, it's not right. When I push values directly into $provincie (PHP array), the script doesn't work anymore. Why ??? How can I give some feedback to the primary PHP page, which calls the JavaScript script, which calls 032A_GetProvincie.php, if something goes wrong in 032A_.. ?
[EDIT 2] I don't know exactly why, but after some more fidgeting, array_push() "started" working the way it should work, and my problems were solved. I'm a PHP ROOKIE as well. Well, I started programming in PHP 9 months ago, coming from VisualStudio. There, things are much more simple and clear.