Hi, i have a bootstrap 3 + php script to add a user to a database. Now what i want is to show a alert message or modal when i got error in script.
Here is my code
<?php
include 'config.php';
// process form
if (isset($_POST['submit'])) {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$email = trim($_POST['email']);
$stmt = $dbh->prepare("SELECT username, email FROM users WHERE username = :username OR email = :email");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':email', $email);
$stmt->execute();
$udata = $stmt->fetch();
if ($udata['username'] == $username) {
$error = '<div class="alert alert-warning alert-dismissible fade-in">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Username in use, please choose another one.
</div>';
} elseif ($udata['email'] == $email) {
$error = '<div class="alert alert-warning alert-dismissible fade-in">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Email in use, please choose another one.
</div>';
} else {
$error = '<div class="alert alert-success alert-dismissible fade-in">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
All ok, register user.
</div>';
}
}
?>
<?php include 'inc/header.php'; ?>
<section class="content-header">
<h1>
Add user
</h1>
<ol class="breadcrumb">
<li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
<li class="active">Add user</li>
</ol>
</section>
<!-- display error message if any -->
<?php if (!empty($error)) { echo '<section class="content-header">'.$error.'</section>'; } ?>
<!-- Main content -->
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">User data</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="" method="post">
<div class="box-body">
<div class="form-group">
<label for="user">Username</label>
<input type="text" name="username" class="form-control" id="user" placeholder="Enter username" required>
</div>
<div class="form-group">
<label for="pass">Password</label>
<input type="password" name="password" class="form-control" id="pass" placeholder="Enter password" required>
</div>
<div class="form-group">
<label for="mail">Email</label>
<input type="email" name="email" class="form-control" id="mail" placeholder="Enter email" required>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</section>
<?php include 'inc/footer.php'; ?>