You can do something like this:
html file.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="">
<select name="ativo">
</select>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "GET",
url: "select.php",
success: function(resposta) {
$("form select[name='ativo']").html(resposta);
}
});
});
</script>
</body>
</html>
Within the second tag script
has two functions, the first $(document).ready(function() {
wait until the entire page is loaded to execute the code block.. The second one $.ajax({
will make an AJAX request to the dynamic file in your application to get the select options you want popular.
And in the select.php file that will be responsible for generating the options, you do this:
select.php
<?php
while($reg = $query->fetch_array()) {
echo '<option value="'.$reg["id"].'">'.$reg["prof"].'</option>'.PHP_EOL;
}
?>
NOTE: The first script tag is the one that includes in the document the jquery plugin, without it the script does not work.
Why? What is the reason? There is more ai is a PHP merge with Javascript?
– novic
The reason would be to make the page more dynamic and clean.
– Amaral
Amaral gives independent of the extension, for example, you can load your page normally and with Ajax load the
Select
your very broad context can really put what you need?– novic
FOR EXAMPLE: I HAVE A COLD-BLOODED SEARCH FORM WHERE YOU HAVE AN INPUT AND A BUTTON NORMALLY AND A CLASS WHERE YOU SHOW THE RESULT BY PULLING FROM THE BANK.
– Amaral
You can create a simple html with ajax to take information from another place from time to time and add this information dynamically in select
– Denis Rudnei de Souza