sql >> Base de Datos >  >> RDS >> Mysql

Conservar los valores de campo después de enviar

Sí, podría implementar (algún tipo de) una sesión flash en este caso:

Así que al enviar:

<?php

session_start(); // don't forget
require_once('../scripts/connect.php');

$con = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME) or die('Could not connect to database server.');

if(isset($_POST['submit'])) {

    $var_Email1 = mysqli_real_escape_string($con, $_POST['field_email1']);
    $var_Email2 = mysqli_real_escape_string($con, $_POST['field_email2']);

    if ($var_Email1 == $var_Email2){

        $sql = mysqli_query($con, "SELECT * FROM membership WHERE Email = '$var_Email1' "); 
        if(mysqli_num_rows($sql) > 0){
            // set session
            $_SESSION['email_exists'] = $var_Email1;
            header('Location: the_starting_php.php');
            exit;
        }
    }

    echo "not in database";

}
?>

Y luego, en la página del formulario, agregue esto también:

<?php
session_start();

// check if there is
$email = '';
if(isset($_SESSION['email_exists'])) {
    $email = $_SESSION['email_exists'];
    unset($_SESSION['email_exists']); // unset it
    echo "
      <script type='text/javascript'>
      alert('The email address $email already exists');
      </script>
    ";
}

?>


<form action="register_script2.php" method="POST" name="form_register" id="form_registerID" accept-charset="UTF-8">

    <aside class="field_reg_form">
          <input name="field_email1" type="text" required id="field_email1ID" value="<?php echo $email; ?>" />
          <br /><br />
          <input name="field_email2" type="text" required id="field_email2ID" value="<?php echo $email; ?>" />
          <br /><br />
          <input type="submit" value="submit" id="submit" name="submit" />
    </aside>

</form>

Nota al margen:sugiero usar declaraciones preparadas.

<?php

session_start(); // don't forget
require_once('../scripts/connect.php');

$con = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME) or die('Could not connect to database server.');

if(isset($_POST['submit'])) {

    $var_Email1 = $_POST['field_email1'];
    $var_Email2 = $_POST['field_email2'];

    if ($var_Email1 == $var_Email2){

        $sql = 'SELECT * FROM membership WHERE Email = ?'; 
        $select = $con->prepare($sql);
        $select->bind_param('s', $var_Email1);
        $select->execute();
        if($select->num_rows > 0){
            // set session
            $_SESSION['email_exists'] = $var_Email1;
            header('Location: the_starting_php.php');
            exit;
        }
    }

    echo "not in database";

}
?>