HTML5 PHP data without refresh?

Asked

Viewed 476 times

-2

As I could make this same SELECT to be executed in.html file and not.php file, I could puck the data of a php with javascript, jquery or something of the type just did not want the extension PHP.

How can I do that?

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

  • Why? What is the reason? There is more ai is a PHP merge with Javascript?

  • The reason would be to make the page more dynamic and clean.

  • 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?

  • 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.

  • 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

1 answer

0

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.

  • If you want to further decrease this file html, you can take everything you have between <script type="text/javascript"> and </script> and save inside a separate file select.js.

  • I didn’t even test but I believe that’s right, and I’m already thanking Valeu even You saved some 5 days of my life. I’ll test and put answer. Thanks Whatyson Neves - Who knows, you know, picks up the explanations in the air! Nevesexpert!

  • Neves, there’s only one small problem, if you can still give it that strength... The function of select is registering in DB the ID and not the option chosen in the form, what I can do?

  • Do you want to add $reg[prof] ? just delete that snippet value="'.$reg["id"].'".

  • PERFECT!!! THANKS Whatyson YOU ARE FUCKING!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.