How to retrieve the option in the click and select the item that is in its value?

1
<div class="escolher_temas">
    <form action="iframe.php" method="GET">
      <select name="escolhe_tema">
        <option value="http://z05.axitech.com.br">Tema 1</option>
        <option value="http://z06.axitech.com.br">Tema 2</option>
        <option value="http://z07.axitech.com.br">Tema 3</option>
        <option value="http://z08.axitech.com.br">Tema 4</option>
        <option value="http://z09.axitech.com.br">Tema 5</option>
        <option value="http://z10.axitech.com.br">Tema 5</option>
        <option value="http://z11.axitech.com.br">Tema 5</option>
        <option value="http://z12.axitech.com.br">Tema 5</option>
        <option value="http://z13.axitech.com.br">Tema 5</option>
        <option value="http://z14.axitech.com.br">Tema 5</option>
        <option value="http://z15.axitech.com.br">Tema 5</option>
        <option value="http://z16.axitech.com.br">Tema 5</option>
        <option value="http://z17.axitech.com.br">Tema 5</option>
        <option value="http://z18.axitech.com.br">Tema 5</option>
        <option value="http://z19.axitech.com.br">Tema 5</option>
        <option value="http://z20.axitech.com.br">Tema 5</option>
      </select>
    </form> 
</div>

<iframe style="height: 587px;" class="full-screen-preview__frame" src="<?php echo $temaselecionado; ?>" name="preview-frame" noresize="noresize" frameborder="0">
</iframe>

I would like to retrieve the value of the option in the click and play it in $temaselecionado to load the chosen page. Notice that the variable is the source of the iframe. If you can do this without PHP , that's fine too.

Someone to help me?

    
asked by anonymous 30.05.2015 / 14:34

2 answers

2

You can do this:

document.querySelector('select[name="escolhe_tema"]').addEventListener('change', function(){
   var iframe = document.querySelector('iframe');
    iframe.src = this.value;
});

This code will detect the change event when an option changes and applies the value of the select in iFrame.

It will assign to the src of select the url that is in the value. However these suggested links seem to not be shown inside an iFrame . If this domain is from the same domain as the parent page you have to use relative paths, otherwise you will not have to load the page in PHP and "copy" the content.

    
30.05.2015 / 14:43
1

With jQuery, you can do this:

$("select[name=escolhe_tema]").change(function() {
    var $iframe = $("iframe[name=preview-frame]");
    $iframe.attr("src", $(this).val());
});
    
30.05.2015 / 15:06