Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if(isset($_POST['submit']))
- {
- if (empty($_POST["firstName"])) {
- $Err[] = "* First Name is required";
- } else {
- $name = test_input($_POST["firstName"]);
- // check if name only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
- $Err[] = "Only letters are allowed in First Name";
- }
- }
- if (empty($_POST["email"])) {
- $Err[] = "* Email is required";
- } else {
- $email = test_input($_POST["email"]);
- // check if e-mail address syntax is valid
- if (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)) {
- $Err[] = "Invalid email format";
- } else {
- $emailSQL = "SELECT email FROM userdetails WHERE email = ?";
- $SQ = $conn->prepare($emailSQL) or die("ERROR: " . implode(":", $conn->errorInfo()));
- $SQ->bindParam(1, $email);
- $SQ->execute();
- $result = $SQ->fetch();
- if($result > 0) {
- $Err[] = "Sorry, email is already in use by another user";
- }
- }
- }
- if (empty($_POST["surname"])) {
- $Err[] = "* Surname is required";
- } else {
- $surname = test_input($_POST["surname"]);
- // check if name only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
- $Err[] = "Only letters are allowed in Surname";
- }
- }
- if (empty($_POST["password"])) {
- $Err[] = "* Password is required";
- } else {
- $password = test_input($_POST["password"]);
- }
- if (empty($_POST["userName"])) {
- $Err[] = "* Username is required";
- } else {
- $username = test_input($_POST["userName"]);
- // check if name only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
- $Err[] = "Only letters are allowed in Username";
- }
- }
- }
- ?>
- <form>Full name:
- <input type="text" name="name" /><span class="error" id="nameError"></span>;
- Email Address:
- <input id="emailCheck" type="text" name="email" /><span class="error" id="emailError">
- </form>
- //add in your connection to the database here, mine is just a require_once statement
- //get the passed parameter
- $email = mysql_real_escape_string(strtolower($_POST["email"]));
- //send a request to the database
- $sql = "SELECT email FROM your_dbtable_name WHERE LOWER(email) = '" . $email . "'";
- $result = mysql_query($sql, $conn) or die("Could not get email: " . mysql_error());
- if(mysql_num_rows($result) > 0) {
- //email is already taken
- echo 0;
- }
- else {
- //email is available
- echo 1;
- }
- <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
- $(document).ready(function() {
- //turns off autocomplete for browsers on email field
- $('#emailCheck').attr('autocomplete', 'off');
- //listens for typing on the desired field
- $("#emailCheck").keyup(function() {
- //gets the value of the field
- var email = $("#emailCheck").val();
- //displays a loader while it is checking the database
- $("#emailError").html("<img alt="" src="../images/loader.gif" />");
- //here is where you send the desired data to the PHP file using ajax
- $.post("../php/checkavailability.php", {email:email},
- function(result) {
- if(result == 1) {
- //the email is available
- $("#emailError").html("Available");
- }
- else {
- //the email is not available
- $("#emailError").html("Email not available");
- }
- });
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement