用户注册失败后保留表单值

时间:2016-08-10 04:04:28

标签: php mysql sql

PHP代码请求

嘿,我想问一下“如果我们注册失败后如何保留表格价值?”

这是我的register.php

<?php
ob_start();
session_start();
if( isset($_SESSION['user2'])!="" ){
 header("Location: home.php");
}
include_once 'serverconfig.php';

if(isset($_POST['btn-signup'])) {
$alpha = "abcdefghijklmnopqrstuvwxyz";
$alpha_upper = strtoupper($alpha);
$numeric = "0123456789";
$special = ".-+=_,!@$#*%<>[]{}";
$chars = "";

if (isset($_POST['gencode'])){
    // if you want a form like above
    if (isset($_POST['alpha']) && $_POST['alpha'] == 'on')
        $chars .= $alpha;

    if (isset($_POST['alpha_upper']) && $_POST['alpha_upper'] == 'on')
        $chars .= $alpha_upper;

    if (isset($_POST['numeric']) && $_POST['numeric'] == 'on')
        $chars .= $numeric;

    if (isset($_POST['special']) && $_POST['special'] == 'on')
        $chars .= $special;

    $length = $_POST['length'];
}else{
    // default [a-zA-Z0-9]{9}
    $chars = $alpha . $alpha_upper . $numeric;
    $length = 9;
}

$len = strlen($chars);
$gc = '';

for ($i=0;$i<$length;$i++)
        $gc .= substr($chars, rand(0, $len-1), 1);

// the finished password
$gc = str_shuffle($gc);

 $namalengkap_ = trim($_POST['namalengkap']);
 $email_ = trim($_POST['email']);
 $nomorhape_ = trim($_POST['nomorhape']);
 $jeniskelamin_ = trim($_POST['jeniskelamin']);
 $tanggallahir_ = trim($_POST['tanggallahir']);
 $username_ = trim($_POST['uname']);
 $password_ = trim($_POST['upass']);

 $namalengkap = strip_tags($namalengkap_);
 $email = strip_tags($email_);
 $nomorhape = strip_tags($nomorhape_);
 $jeniskelamin = strip_tags($jeniskelamin_);
 $tanggallahir = strip_tags($tanggallahir_);
 $username = strip_tags($username_);
 $password = strip_tags($password_);

 // check email exist or not
 $query = "SELECT email FROM user2 WHERE email='$email'";
 $result = mysql_query($query);
 $query2 = "SELECT username FROM user2 WHERE username='$username'";
 $result2 = mysql_query($query2);

 $count = mysql_num_rows($result); // if email not found then proceed
 $count2 = mysql_num_rows($result2);

 if ($count==0 && $count2==0) {
  $query = "INSERT INTO user2(namalengkap,email,jeniskelamin,tanggallahir,username,password,activecode,status) VALUES('$namalengkap','$email','$jeniskelamin','$tanggallahir','$username','$password','$gc','0')";
  $res = mysql_query($query);

  if ($res) {
   $errTyp = "success";
   $errMSG = "Registrasi Berhasil !";
  } else {
   $errTyp = "danger";
   $errMSG = "Ada Kesalahan Saat Mengisi Form Di Bawah, Silahkan Cek Kembali";
  } 

 } else {
  $errTyp = "warning";
  $errMSG = "E-Mail/Username Sudah Digunakan Oleh User Lain. Silahkan Daftar E-Mail,&nbsp;";
  $errMSG .= "<a href=\"mail/register.php\" target=\"_blank\">Klik Disini</a>";
 }
}
?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Ariigoh Register</title>
        <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
    </head>

    <body>

        <div class="container">

            <div id="login-form">
                <form method="post" autocomplete="off">

                    <div class="col-md-12">

                        <div class="form-group">
                            <h2 class="">Sign Up to <kbd>Ariigoh Afnan Localhost</kbd></h2>
                        </div>

                        <div class="form-group">
                            <hr />
                        </div>

                        <?php
   if ( isset($errMSG) ) {

    ?>
                            <div class="form-group">
                                <div class="alert alert-<?php echo ($errTyp==" success ") ? "success " : $errTyp; ?>">
                                    <span class="glyphicon glyphicon-info-sign"></span>
                                    <?php echo $errMSG; ?>
                                </div>
                                <div id="errmsgbox">

                                    <div id="divError"></div>

                                </div>
                            </div>
                            <?php
   }
   ?>
                                <div class="form-group">
                                    <div class="input-group">
                                        <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                                        <input type="text" name="namalengkap" class="form-control" placeholder="Masukan Nama Lengkap Anda" pattern="^[a-zA-Z ]*$" title="Masukan Nama Anda Yang Benar" max="32" min="4" required/>
                                    </div>
                                    <div class="input-group">
                                        <p><kbd>Input tidak di perbolehkan nomor/spesial karakter</kbd></p>
                                    </div>
                                    <div class="input-group">
                                        <span class="input-group-addon"><span class="glyphicon glyphicon-earphone"></span></span>
                                        <input type="text" name="nomorhape" class="form-control" placeholder="Masukan Nomor Handphone Anda" pattern="^[0-9]*$" title="Masukan Nomor Handphone Yang Benar" max="14" min="4" required />
                                    </div>
                                    <div class="input-group">
                                        <p><kbd>Input hanya nomor dan tanpa (+62) -> ex : 08xxxxxxxxxx</kbd>&nbsp;</p>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-group">
                                        <span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span>
                                        <input type="email" name="email" class="form-control" placeholder="Masukan Alamat Email Anda" pattern="^[A-Za-z@.0-9]*$" title="Masukan E-Mail yang Valid(Aktif)" required />
                                    </div>
                                    <div class="input-group">
                                        <p><kbd>Input hanya diperbolehkan a-z(A-Z),@,0-9,dan.(titik) -> ex : contoh.12@localhost.com</kbd></p>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="sel1">Jenis Kelamin:</label>
                                    <select class="form-control" id="sel1" name="jeniskelamin" required>
                                        <option value="Laki-Laki">Laki-Laki</option>
                                        <option value="Perempuan">Perempuan</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="sel2">Tanggal Lahir :</label>
                                    <div class="input-group">
                                        <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                                        <input type="date" name="tanggallahir" class="form-control" required />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-group">
                                        <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                                        <input type="text" name="uname" class="form-control" placeholder="Masukan Username" pattern="^[a-zA-Z0-9]*$" title="Masukan Username Dengan Benar" required />
                                    </div>
                                    <div class="input-group">
                                        <p><kbd>Input hanya diperbolehkan a-z(A-Z) dan 0-9</kbd></p>
                                    </div>
                                    <div class="input-group">
                                        <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
                                        <input type="password" name="upass" class="form-control" placeholder="Masukan Password" pattern="^[a-zA-Z0-9]*$" title="Masukan Password Dengan Benar" required />
                                    </div>
                                    <div class="input-group">
                                        <p><kbd>Input hanya diperbolehkan a-z(A-Z) dan 0-9</kbd></p>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="alert-danger">
                                        <h4><strong>Perhatian !</strong>&nbsp;Gunakan E-Mail Yang Sudah Terdaftar di Localhost. Kalau Tidak Yakin Silahkan Daftar, <a href="mail/register.php">Klik Disini</a></h4>
                                    </div>
                                </div>
                                <div id="form-group">
                                    <button type="submit" class="btn btn-block btn-primary" name="btn-signup">Sign Up</button>
                                </div>
                                <hr>
                                <div class="form-group">
                                    <a href="index.php">Sign in Here...</a>
                                </div>

                    </div>

                </form>
            </div>
        </div>

    </body>

    </html>

我该怎么办这个剧本?

  

谢谢你的帮助:)抱歉我的英文不好;)

2 个答案:

答案 0 :(得分:0)

看看此示例可能会对您有所帮助PHP Form validation

答案 1 :(得分:0)

使用这样的东西:

<input type="email" name="email" class="form-control" placeholder="Masukan Alamat Email Anda" pattern="^[A-Za-z@.0-9]*$" title="Masukan E-Mail yang Valid(Aktif)" value="<?php isset($_REQUEST['email']) ? $_REQUEST['email'] : '' ?>" required />

value="<?php if(isset($_REQUEST['email'])) { echo $_REQUEST['email']; } ?>"

将在文本框中保留不成功处理的值。