Open Poup-Up - Help

0

I have the following HTML, but everything on the same page id entry is where it gets my input field from the typed address arrival is where it gets the final address result is the calculation:

    <div id="entrada"></div>
    <div id="chegada"></div>
    <div id="Resultado"></div>

<input type="submit" class="btn-green" id="btnCalcular" onclick="calcular();" value="Calcular" />

I have this function to calculate my input text:

<script language= "javascript">
function calcular(){
            var litros = parseFloat(document.getElementById('txtKM').value) / parseFloat(document.getElementById('txtKML').value);
            var valor = parseFloat(document.getElementById('txtPL').value) * litros;
            document.getElementById('Resultado').innerHTML = "<br><b>Gasto total: R$ </b>" + valor;
            var txtentrada = document.getElementById('txtEnderecoPartida').value;
            document.getElementById('entrada').innerHTML = "<br><b>Endereço de Partida:</b><br>" + txtentrada;
            var txtchegada = document.getElementById('txtEnderecoChegada').value;
            document.getElementById('chegada').innerHTML = "<br><b>Endereço de Chegada:</b><br>" + txtchegada;
        }
</script>

Now, what I wanted to know is: How do I click the button to open it on another page (poup-up), already tried with windows.open but does not open, since my onclick is calling this function on the same page to calculate.

EDIT: I have this HTML:

<a href="index.php" onclick="window.open('index.php', 'Pagina', 'STATUS=NO, TOOLBAR=NO, LOCATION=NO, DIRECTORIES=NO, RESISABLE=NO, SCROLLBARS=YES, TOP=10, LEFT=10, WIDTH=900, HEIGHT=500');">

I wanted to pull the result information to a new page;

    
asked by anonymous 31.03.2018 / 19:07

2 answers

1

You can send the value to the new popup window via POST, creating a dynamic form fault.

Link to open popup :

<a href="javascript:void(0)" onclick="novaJanela()">Abrir janela</a>

Function:

function novaJanela(){

   // seleciono o form para ver se ele existe
   var popupform = document.body.querySelector("#popupform");

   // se ele existe, excluo do DOM
   if(popupform) popupform.outerHTML = '';

   // pego o texto na div #Resultado
   var res = document.getElementById("Resultado").textContent;

   var f = document.createElement("form"); // crio o form
   f.method = "post"; // adiciono método post
   f.target = "Pagina"; // defino o alvo para a popup
   f.action = "index.php"; // defino o action para o index.php da popup
   f.id = "popupform"; // defino uma id ao form
   f.style.cssText = "display: none;"; // escondo o form para que funcione em background

   var i = document.createElement("input"); // crio o input do form que levará o valor
   i.value = res; // adiciono o valor ao input
   i.name = "resultado"; // dou um nome ao input que será capturado pelo PHP
   f.appendChild(i); // adiciono o input ao form

   document.body.appendChild(f); // adiciono o form ao DOM

   // abro a popup
   window.open('index.php', 'Pagina', 'STATUS=NO, TOOLBAR=NO, LOCATION=NO, DIRECTORIES=NO, RESISABLE=NO, SCROLLBARS=YES, TOP=10, LEFT=10, WIDTH=900, HEIGHT=500');

   f.submit(); // submeto o form à popup
}

In popup index.php you get the value using POST:

<?php
$resultado = $_POST['resultado'];
?>
    
01.04.2018 / 22:57
0

You can do the following: put in href of link <a> :

href="javascript:void(0)"

This will prevent link redirection action. And put in onclick a function to open a new window:

onclick="novaJanela()"

The <a> will look like this:

<a href="javascript:void(0)" onclick="novaJanela()">Abrir janela</a>

Once this is done, add the novaJanela() function in the script:

function novaJanela(){

   var res = document.getElementById("Resultado").innerHTML;

   window.open('index.php?resultado='+res, 'Pagina', 'STATUS=NO, TOOLBAR=NO, LOCATION=NO, DIRECTORIES=NO, RESISABLE=NO, SCROLLBARS=YES, TOP=10, LEFT=10, WIDTH=900, HEIGHT=500');

}

The variable var res will store the content of div Resultado and send as a parameter " result " to the popup :

window.open('index.php?resultado='+res,...

In the popup index.php you get the value using GET:

<?php
$resultado = $_GET['resultado'];
?>
  

This mode of opening the popup will not be blocked by the browser because   was a result of user interaction through a click.

    
31.03.2018 / 22:09