PHP link on Submit button

Asked

Viewed 438 times

-1

Boas, I need a PHP function, as I cannot, to list files with a certain extension,(html) and put them in a combo box, or Dropbox. Then I need to select a file and press the button, open the corresponding file. Right now, everything works except that when pressing the button appears the link I have to press to open the file. how to do to open soon as you press the boot ?

Thank you!

<html>
<head>
</head>
<body>
<form action="#" method="post">
<?php
$files = glob('*.html');
echo "<select name='datas'>";
foreach ($files as $file) {
echo "<option>".$file."</option>"; }
echo "</select>";
?>
<input type="submit" name="submit" value="Seleccione a Data" />
</form>

<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['datas'];
echo "<a href=$selected_val>$selected_val</a>";
}
?>

</body>
</html>
  • 3

    Young here is the PT.Stackoverflow, please edit your question by writing in Portuguese.

  • 1

    Translated. Thank you for the reminder,

1 answer

0

Only with PHP could you use the function header('Location:' . $endereco).

Would look like this in PHP:

<?php
    if(isset($_POST['submit'])){
        $selected_val = $_POST['datas'];
        header('Location: ' . $selected_val);
    }
?>

Another alternative is to use a simple Javascript.

A small change in the HTML body

<html>
<head>
</head>
<body>
    <form>
        <?php
            $files = glob('*.html');
            echo "<select id='idcombobox' name='datas'>";
            foreach ($files as $file) {
                echo "<option>".$file."</option>"; 
            }
            echo "</select>";
        ?>

        <input type="button" name="submit" onclick="abrirPagina()" value="Seleccione a Data" />
    </form>
</body>

Javascript

<script language="javascript">
    function abrirPagina(){
        console.log("teste");
        var combobox = document.getElementById("idcombobox");
        var itemSelecionado = combobox.options[combobox.selectedIndex].text;
        window.location.href = itemSelecionado;
    }
</script>

Browser other questions tagged

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