Manage variables with PHP session

5

I need to get the value of an input text to store it and use it on another page that pops up automatically when the form's Submit button is pressed.

He can do this, but what is happening is that when I press the button the first time it does not pick up, when I press to send the form a second time it sends the value that was in the first and so on ... ideas?

    
asked by anonymous 02.12.2015 / 13:27

1 answer

2

Your code is wrong. You are setting the value of $_SESSION['nome'] with a $_POST that does not even exist and the same situation repeats when you call the popup.

Do so

<?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 end of your page, after the </body> tag, place 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>";
    }

?>

In the popup you do not need to change anything.

Just to state: I do not recommend using popup because most modern browsers block all by default, and less savvy users do not know how to make them appear.

    
02.12.2015 / 21:45