Find input value from another php file

0

I'm uploading images to a server folder, using the following code:

<iframe width="600" name="frmfoto" height="300" frameborder="0" src="teste2.php"></iframe>

        <script type="text/javascript">
            function txfoto() {
                    var valor = window.parent.frmfoto.document.getElementById('foto').value;
                    $('#foto').val(valor);
            }

        </script>

        <!-- FOTO 1 -->
        <div class="form-group">
            <label class="col-md-2 control-label" for="fotofundo">* Foto 01</label>  
               <div class="col-md-5">
                   <input id="fotofundo" name="fotofundo" type="text" placeholder="" class="form-control input-md" onClick="txfoto()" />
               </div>
        </div>

In the file uploading, here's my input:

<input id="foto" name="foto" type="text" class="form-control input-md" value="***************">

The photo goes to the server folder, prints in the input above, but I can not pull it to my other form, when I try to activate the "onClick" event in the input.

What could be wrong?

    
asked by anonymous 23.02.2018 / 13:59

2 answers

0

The window.parent is not necessary, because window.parent will search the page above, as the script is already running on the page above so it is irrelevant, just get the <iframe> .

To not conflict with global variables (in the scope of window. ) it would be more interesting to use jQuery itself, in the jQuery selector you can change the context, using .contentWindow , like this:

 $("<seletor>", <janela>.contentWindow.document);
  

Note: <seletor> and <janela> are examples and not code

Another problem is that you on the above page are using fotofundo and not foto , so change this:

$('#foto').val(valor);

For this reason:

$('#fotofundo').val(valor);

The whole code should look like this:

<iframe width="600" name="frmfoto" height="300" frameborder="0" src="teste2.php"></iframe>

<script>
function txfoto() {
     //Pega o iframe
     var frmfoto = $("iframe[name=frmfoto]");

     //Pega o input dentro do iframe
     var input = $("#foto", frmfoto.get(0).contentWindow.document);

     //Passa o valor do input do iframe para o input na janala principal
     $('#fotofundo').val( input.val() );
}
</script>

<!-- FOTO 1 -->
<div class="form-group">
    <label class="col-md-2 control-label" for="fotofundo">* Foto 01</label>  
       <div class="col-md-5">
           <input id="fotofundo" name="fotofundo" type="text" placeholder="" class="form-control input-md" onClick="txfoto()" />
       </div>
</div>
    
23.02.2018 / 17:26
0

There are two problems in the code:

One is here:

window.parent.frmfoto.document.getElementById('foto').value;

To get the value of an element in parent-document , you should use window.parent.document :

window.parent.document.getElementById('foto').value;

The other is here:

$('#foto').val(valor);

In this id #foto is non-existent. The correct would be to get id #fotofundo using pure JavaScript (this prevents you from loading jQuery into the frame just to do this):

document.getElementById('fotofundo').value = valor;
    
23.02.2018 / 17:09