Open Poup-Up - Help

Asked

Viewed 366 times

0

I have the following HTML, but all on the same page id entree is where it takes my input field from the address typed arrival is where he gets his final address outworking 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’d like to know is: How do I stop at the time of clicking the button it can open 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 information from outworking to a new page;

  • For the pop-up you need to pass an additional parameter, have you tried?: window.open ('popup.html', 'popup')

  • Note that the browser blocks pop, need to release the first time.

  • And to send the result to the new page in the pop-up, you can pass via get or post. via get would be simple: window.open ('index.php?par1=2&par2=3', 'popup')

  • Put all html if given, ta kind of confused. Where are these elements with id txtKM and txtPL?

  • Leandro, this would be my inputs with the respective ids to take the distance in km, fuel consumption in km and fuel price to at the end click the calculate button and get the result, so when clicking it shows on the page and was quenrendo open in a new window (Pou-up).

2 answers

1

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

Link to open the 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 the popup index.php you take the value using POST:

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

0

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

href="javascript:void(0)"

This will prevent redirect action of the link. And put in the 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>

Done this, add function novaJanela() 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 contents of div Resultado and send as parameter "outworking" to the popup:

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

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

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

This way of opening the popup will not be browser blocked because was the result of user interaction through one click.

  • So far so good, but, from what I understand it takes the value generated in result and shows in the link, for example: meusite/index.php? result=<b>Spend%20total:%20R$%20</b>19.764705882352942 .

  • Use textContent instead of innerHTML

  • 1

    Got it. You don’t want the value to appear in the URL?

  • No, but it helps. But, I would like to pull the informed value of my result div, to another page, as soon as clicked on the button, open a page or Poup PS: now that I saw what posted above, I will see!

  • I didn’t even notice one thing: you want to do it using PHP?

  • Then, look at this image: http://www.liguetaxi.com.br/Capturar.PNG <div id="input"></div> <div id="arrival"></div> <div id="Result"></div> These 3 Divs are the ones that appear after I click Calculate. So I wish I could appear on another page. Sorry for the misunderstanding

  • I understood that you want to play the result value to another page. But you have control of the other page code?

  • So, here’s the problem. I don’t know where to start, but would you have an example page to make something simulate, be it in php or javascript? I would have to create the other page so that I could pull that information? the Code itself, is on the same page.

Show 3 more comments

Browser other questions tagged

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