How to return a validated array to a form?

Asked

Viewed 196 times

0

I have an application system step-by-step that would be step by step, ie first the user informs some data then others and others... I want to validate this data with PHP (because I have to query the database to see if the user already exists). I have the following code:

<form  method="post" action="forms/register.php">
    <div class="modal-content modal-person modal-person2">
        <div class="modal-header">
            <h4 class="modal-title md-title">Registrar</h4>
            <h5 class="modal-title md-title">login</h5>
        </div>

        <div class="modal-body">
            <?php
            if (isset($_REQUEST['error'])) {
                $erro = $_REQUEST['error'];
                if ($erro == "userexist") {
                    echo "<b class=\"text-danger\">Usuário ou email já cadastrado!</b>";
                    echo "<p class=\"text-danger\">Veja-os novamente..<br><br>";
                } else if ($erro == "passdistint") {
                    echo "<b class=\"text-danger\">Senhas são distintas!</b>";
                    echo "<p class=\"text-danger\">Os campos de senha devem ter a mesma senha..<br><br>";
                } else if ($erro == "every") {
                    echo "<b class=\"text-danger\">Erro desconhecido!</b>";
                    echo "<p class=\"text-danger\">Tente novamente mais tarde...<br><br>";
                } else if ($erro == "sucess") {
                    echo "<script>
                    $(function(){
                        if($('.ativo').next().size())
                        {
                            $('.ativo')
                                .hide(800)
                                .removeClass('ativo')
                                .next()
                                .show(800)
                                .addClass('ativo');
                        }
                     });</script>";
                }
            }
            ?>

            <div class="input-group md-input">
                <span class="input-group-addon pq-txt" id="basic-addon1">웃</span>
                <input type="text" class="form-control" maxlength="15" id="user" name="user" required="" placeholder="Usuário">
            </div>

            <div class="input-group md-input">
                <span class="input-group-addon pq-txt" id="basic-addon1">@</span>
                <input type="email" class="form-control" name="email" required="" placeholder="Email">
            </div>

            <div class="input-group md-input">
                <span class="input-group-addon md-txt" id="basic-addon1">#*</span>
                <input type="password" class="form-control" maxlength="15" id="senha" required="" name="pass1" placeholder="Senha">
                <span class="input-group-addon md-txt confirm" id="basic-addon1"><span class="confirm" >#*</span></span>
                <input type="password" class="form-control confirm" maxlength="15" id="senha2" required=""  name="pass2" placeholder="Confirmar Senha">
            </div>
            <input type="hidden" name="step" value="1">
            <div class="barprogress" >
                <div></div>
            </div><br>
        </div>
        <div class="modal-footer">

            <input type="submit" class="btn btn-success md-btn" value="Próximo"/>
            <div class="progress">
                <div class="progress-bar progress-bar-info" style="width: 33.3%">
                </div>
            </div>
        </div>
    </div>


    <div class="modal-content modal-person modal-person2">
        <div class="modal-header">
            <h4 class="modal-title md-title">Registrar</h4>
            <h5 class="modal-title md-title">perfil</h5>
        </div>
        <?php
            if (isset($_REQUEST['error'])) {
                $erro = $_REQUEST['error'];
                if ($erro == "userexist") {
                    echo "<b class=\"text-danger\">Usuário ou email já cadastrado!</b>";
                    echo "<p class=\"text-danger\">Veja-os novamente..<br><br>";
                } else if ($erro == "passdistint") {
                    echo "<b class=\"text-danger\">Senhas são distintas!</b>";
                    echo "<p class=\"text-danger\">Os campos de senha devem ter a mesma senha..<br><br>";
                } else if ($erro == "every") {
                    echo "<b class=\"text-danger\">Erro desconhecido!</b>";
                    echo "<p class=\"text-danger\">Tente novamente mais tarde...<br><br>";
                } else if ($erro == "sucess2") {
                    echo "<script>
                    $(function(){
                        if($('.ativo').next().size())
                        {
                            $('.ativo')
                                .hide(800)
                                .removeClass('ativo')
                                .next()
                                .show(800)
                                .addClass('ativo');
                        }
                     });</script>";
                }
            }
            ?>
        <div class="modal-body">
            <center><img src="..." alt="..." width="100" height="100" class="img-circle perfil"></center><br>
            <div class="input-group pq-input">
                <span class="input-group-addon pq-txt" id="basic-addon1">웃</span>
                <input type="file" class="form-control btn btn-default md-btn fileimg" accept="image/*"  name="img" required="" >
            </div>

            <div class="input-group md-input">
                <span class="input-group-addon md-txt" id="basic-addon1">Nome</span>
                <input type="text" class="form-control" maxlength="200" required="" name="nome" placeholder="Nome Completo">
            </div>

            <div class="input-group md-input">
                <span class="input-group-addon md-txt" id="basic-addon1">Nascimento</span>
                <input type="date" class="form-control" required="" name="data">
                <span class="input-group-addon md-txt" id="basic-addon1">Profissão</span>
                <input type="text" class="form-control" maxlength="100" required=""  name="profissao" placeholder="Trabalho em..">
            </div>
            <input type="hidden" name="step" value="2">
        </div>
        <div class="modal-footer">
            <input type="submit" class="btn btn-success md-btn" value="Próximo"/>
            <div class="progress">
                <div class="progress-bar progress-bar-info" style="width: 66.6%">
                </div>
            </div>
        </div>
    </div>
</form>

The first part (where the user informs login, email and password) is confirmed thus:

$mnpdo = new MinPDO();
$step = $_REQUEST['step'];
if($step == 1) {
    if(isset($_REQUEST['user']) and isset($_REQUEST['email'])) {
        $user = $_REQUEST['user'];
        $email = $_REQUEST['email'];
        try {
            $result = $mnpdo->consult("users", "*", "email = '$email' or username = '$user'");
            if($result[0]["username"] == $user or $result[0]["email"] == $email) {
                header("location:../singup.php?error=userexist");
                exit();
            }    
        } catch (MinPDOException $ex) {
            if($ex->getMessage() != "No results!") {
                header("location:../singup.php?error=".$ex->getMessage());
                exit();
            }

        }

        if(isset($_REQUEST['pass1'])) {
            $pass1 = $_REQUEST['pass1'];

            if(isset($_REQUEST['pass2'])) {
                $pass2 = $_REQUEST['pass2'];
                if($pass1 != $pass2) {
                    header("location:../singup.php?error=passdistint");
                    exit();
                } else {
                    header("location:../singup.php?error=sucess");
                    exit();
                }
            } else {
                header("location:../singup.php?error=pass2");
                exit();
            }
        } else {
            header("location:../singup.php?error=pass");
            exit();
        }
    } else {
        header("location:../singup.php?error=user");
        exit();
    }
}

When it validates return sucess and so I do the action to go to the next step..

How do I stop when he comes back with the sucess or even failed it does not lose the data that has been validated?

I was thinking as I validate go storing the values in an array $validado['meuinput'] and when returning with head take the array values, but how do I pass the array to the page? (not by url - get)

  • 1

    I think you can use session variables.

2 answers

3


You can store the array in a session variable... The session variables can be accessed from any page, which would solve the problem of information transit... Ex:

    $_SESSION["nomeDaVariavel"] = $seuArray;

With each new item you add to the array, you will need to update the session variable using the same command above.

When any failure occurs in the authentication process, you destroy the session variable. Ex:

    if($result[0]["username"] == $user or $result[0]["email"] == $email) {
            header("location:../singup.php?error=userexist");
            unset($_SESSION['nomeDaVariavel']);
            exit();
        } 
  • 1

    Hello Andre, I think you could improve this answer, including some explanation, and an example of how to do this.

  • 2

    I updated the answer, @gustavox. Really, I was weak... Thanks for the feedback... =)

  • It was cool André. + 1

1

You can use $_SESSION, once you enter step validation saves to session:

session_start(); $_SESSION["meuinput"] = "valor"; $_SESSION["meuinput2"] = "2"; session_write_close();

Dai after redirecting on the form screen just input the data:

<?php

session_start();

?>

<input type="text" value="<?=isset($_SESSION['meuinput'])?$_SESSION['meuinput']:'';?>">

  • Missed the echo in value.

  • Actually did not miss, because the <?= is the same as <?php echo

  • I tried it like this, accused error in = '-'

  • 1

    Ahhh, it makes sense what you said then, because to use the = depends on the configuration of PHP, in mine here is working because my configuration allows. Thanks

Browser other questions tagged

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