Fetch input value from another php file

Asked

Viewed 76 times

0

I am 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 that makes the upload, here is 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 above input, but I can’t pull it to my other form, when I try to activate the "onClick" event in the input.

What could be wrong?

  • You’ll get better answers if you give people code they can use to reproduce the problem.

  • Put here or in the Pastebin?

  • Wherever you think best.

2 answers

0

There are two problems in the code:

One is here:

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

To take the value of an element in parent document, should wear window.parent.document:

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

The other is here:

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

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

document.getElementById('fotofundo').value = valor;
  • Thanks for the help. I had not seen your post, I ended up using the solution below.

0


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

Not to 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, in this way:

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

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

Another problem is that you on the page above this using fotofundo and not foto, then 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>
  • It was perfect, thanks. Just one more thing: Is there any way I can update the input without the click? You know how user is...

  • @Hebertrichardmassenodias try to use .load in the <iframe>

  • my knowledge is very little in javascript. Could you give me an example or how I should search?

  • @Hebertrichardmassenodias puts so on your page $(function () { $("iframe[name=frmfoto]").on("load", txfoto) });, I haven’t tried it yet, but I think it should work. Anything creates a new question just about this, if it doesn’t work, which I’ll try to answer.

  • It didn’t work, I’ll create another question. Thank you!

  • https://answall.com/questions/278930/atualizar-input-com-dados-vindo-de-um-iframe-automaticamente

Show 1 more comment

Browser other questions tagged

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