Question:
I need to get the value of an input text, store it and use it in another page that opens automatically (popup) when the form's Submit button is pressed.
He can do that, but what's happening is that when I press the button the first time it doesn't take, when I press to send the form a second time it sends the value that was on the first time and so on… ideas?
Answer:
Your code is wrong. You are setting the value of $_SESSION['nome']
with a $_POST
that doesn't even exist yet and the same situation repeats when calling the popup.
Do like this
<?php
session_start('professor');
$chamarPopup = false;
//Verifica se existe um $_POST chamado nomeP, que é o name do seu input.
if (isset($_POST['nomeP']) && !empty($_POST['nomeP'])) {
$_SESSION['nome'] = $_POST['nomeP'];
$chamarPopup = true;
}
?>
<form method="post" name="formTest">
<input type="text" name="nomeP" value="Nome Professor">
<input type="submit" value="PopUp">
</form>
At the bottom of your page, after the </body>
tag put this
<?php
if ($chamarPopup === true) {
echo "<script>window.onload = function(){";
echo "varWindow = window.open ('cadastro_prof_disc.php', 'popup', 'width=1024, height=350, top=300, left=400%, scrollbars=no, resizable=yes,')";
echo "};</script>";
}
?>
No popup does not need to change anything.
Just for the record: I don't recommend using popup, as most modern browsers block all by default and less experienced users don't know how to get them displayed.