HTML5 PHP data without refresh?

-2

How can I do this?

<select name="ativo" action="">
    <?php while($reg = $query->fetch_array()) { ?>
    <option value="<?php echo $reg["id"]?>"> <?php echo $reg["prof"]?> </option>
    <?php }?>
</select>
    
asked by anonymous 23.11.2016 / 18:42

1 answer

0

You can do something like this:

file.html

<!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><scripttype="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() { waits until the entire page is loaded to execute the code block. The second $.ajax({ will make an AJAX request to the dynamic file in your application to pick the select options that 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 tag script is the one that includes in the document the jquery plugin, without it the script does not work.

    
24.11.2016 / 11:18