PHP link on submit button

-1

Good, I need a function in PHP, since in HTML I can not, to list the files with a certain extension, (html) and put them in a combo box, or dropbox. Then I need you to select a file and press the button, open the file. at this moment, everything works except that pressing the button shows the link that I have to press to open the file. how do I open it as soon as the button is pressed?

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>
    
asked by anonymous 28.12.2018 / 11:30

2 answers

0

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

It 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 body of HTML

<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>
    
29.12.2018 / 22:57
-1

JavasScript

function goToPage() {
  if(document.getElementById('target').value){
      window.location.href = document.getElementById('target').value;
  }
}

HTML

<select id="target">

<?php
$files = glob('*.html');

foreach ($files as $file) {

    echo "<option value='".$file."'>".$file."</option>";
}
?>    

</select>
<input type="button" value="Visite Link!"onclick="goToPage()">

Example

function goToPage() {
  if(document.getElementById('target').value){
      window.location.href = document.getElementById('target').value;
  }
}
<select  id="target">

<option value='https://pt.stackoverflow.com/users'>stackoverflow users</option>
<option value='https://pt.stackoverflow.com/questions/tagged/php'>stackoverflow questions</option>  
<option value='https://pt.stackoverflow.com/help'>stackoverflow help</option>   
    
</select>
<input type="button"  value="Visite Link!"onclick="goToPage()">
    
29.12.2018 / 22:41