Home > database >  Go to a precedent page in a php site
Go to a precedent page in a php site

Time:04-29

I'm creating a simple php program and I'm trying to find a way to be able to go from the page 2 back to the page 1 with a button. I tried by making a button in the second page and giving it the value 1 but it gives me error with the user. How can I do it?

<?php
    $servername = "localhost";
    session_start();
        if(!(isset($_POST["pagina"]))){
        echo "
            <h2> Accesso </h2>
            <form action='Login.php' method='post'>
                Inserisci il nome utente e password: <br/>
                Utente:<input type='text' name ='utente' /> <br/>
                Password: <input type='password' name ='pwd'/><br/>
                <input type ='submit' value='Accedi'/>
                <input type='hidden' name='pagina' value='1' />
            </form>";
    }else if($_POST["pagina"]==1){
        $utente = $_POST["utente"];
        $password = $_POST["pwd"];
        $conn = new mysqli($servername, $utente, $password, 'agenziaviaggi');
        if($conn->connect_errno){
            echo "Connessione impossibile: ".$conn->connect_error;
            exit;
        }
        $_SESSION["utente"] = $utente;
        $_SESSION["pwd"] = $password;
        
        echo "Aeroporto  ";
        echo "<form action ='Login.php' method = 'post'>
            <input type = 'submit' name = 'aeroporto' value = 'Visualizza Aeroporto'>
            <input type = 'hidden' name = 'pagina' value = '2'/>
        </form>";   
    }
    else if($_POST["pagina"]==2){
        $utente = $_SESSION["utente"];
        $password = $_SESSION["pwd"];
        $conn = new mysqli($servername, $utente, $password, 'agenziaviaggi');
        if($conn->connect_errno){
            echo "Connessione impossibile: ".$conn->connect_error;
            exit;
        }
        switch(true){
            case isset($_POST['aeroporto']):
                echo 'Aeroporto<br/>' ;
                $sql = "SELECT Nome, Città, Servizi, Parcheggi, Telefono FROM aeroporto";
                $result = $conn->query($sql);

                if ($result->num_rows > 0) {
                    while($row = $result->fetch_assoc()) {
                        echo "<br> Nome: ". $row["Nome"]. " - Città: ". $row["Città"]. " - Servizi: ". $row["Servizi"]. " - Parcheggi: ". $row["Parcheggi"]. " - Telefono: ". $row["Telefono"]. "<br>";
                    }
                } else {
                    echo "0 results";
                }

                echo "<form action ='Login.php' method = 'post'>
                    <input type = 'submit' value = 'Visualizza info'>
                    <input type = 'hidden' name = 'pagina' value = '1'/>
                </form>";
                break;
        }
    }
    ?>

CodePudding user response:

So, reading your code, on your 'page 2', you have this form:

<form action ='Login.php' method = 'post'>
                    <input type = 'submit' value = 'Visualizza info'>
                    <input type = 'hidden' name = 'pagina' value = '1'/>
                </form>

And your if-else states:

}else if($_POST["pagina"]==1){
        $utente = $_POST["utente"];
        $password = $_POST["pwd"];
        $conn = new mysqli($servername, $utente, $password, 'agenziaviaggi');
        if($conn->connect_errno){
            echo "Connessione impossibile: ".$conn->connect_error;
            exit;
        }
        $_SESSION["utente"] = $utente;
        $_SESSION["pwd"] = $password;

So your session values utente and pwd are replaced by the posted ones, but they aren't posted by your 'page 2'.

Once a user is logged in, and the session values are set, you can also create hyperlinks instead of forms to navigate between pages. But I don't understand your goal completely. Create other questions for that.

  • Related