0

I have this next page, on which a user can create a new account. My question now is how can i make sure the user inserts a username that doesn't already exist in my MySQL database. The page is available on http://webs.hogent.be/kevinbaeyens/gebruiker.html the code of this page is:

<!doctype html>

<html class="">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Nieuwe gebruiker | Sociale buurt</title>
<link href="boilerplate.css" rel="stylesheet" type="text/css">
<link href="onzebuurt.css" rel="stylesheet" type="text/css">

<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" language="javascript">
    window.onload = function() {
      document.getElementById("Username").focus();
    };

    function formulierValideren() {     
            if (document.getElementById('Username').value == '' || document.getElementById('Username').value == null)
            {
                alert ('Gebruikersnaam is verplicht.');
                document.getElementById('Username').style.borderColor = "red";
                return false;
            }
            else if (document.getElementById('Wachtwoord').value == '' || document.getElementById('Wachtwoord').value == null)
            {
                alert ('Wachtwoord is verplicht.');
                document.getElementById('Wachtwoord').style.borderColor = "red";
                return false;
            }
            else if (document.getElementById('Wachtwoord2').value == '' || document.getElementById('Wachtwoord2').value == null)
            {
                alert ('Bevestig wachtwoord.');
                document.getElementById('Wachtwoord2').style.borderColor = "red";
                return false;
            }
            else if (document.getElementById('Wachtwoord2').value != document.getElementById('Wachtwoord').value)
            {
                alert ('Wachtwoorden komen niet overeen.');
                document.getElementById('Wachtwoord2').style.borderColor = "red";
                return false;
            }
            else
            {
                var url = "http://localhost:8080/OnzeBuurt2/resources/gebruikers";

                var reminder = {};
                reminder.naam = jQuery.trim($("#Username").val());
                reminder.wachtwoord = jQuery.trim($("#Wachtwoord").val());

                var request = new XMLHttpRequest();
                request.open("POST", url);
                request.onload = function() {
                    if (request.status === 201) {
                        reminder.id = request.getResponseHeader("Location").split("/").pop();


                    } else {
                        console.log("Error creating reminder: " + request.status + " " + request.responseText);
                    }
                };

                request.setRequestHeader("Content-Type", "application/json");
                request.send(JSON.stringify(reminder));     




                var msg = "Registratie succesvol. Klik op OK om u aan te melden op de site.";
                if(confirm(msg)){
                setTimeout(function() {window.location.href = "http://localhost:8080/OnzeBuurt2/"})
                }

            }
            //end if
    }//end function


</script>
</head>
<body class="body2">
<div class="gridContainer clearfix">
  <div class="header2">
        <center>    
        Nieuwe gebruiker
        </center>
  </div>
        <div id="formulier2">
            <form method="post" name="form" action="">
                <p class="labels"><center>Gebruikersnaam *</center></p><input id="Username" type="text" name="Username" placeholder="Gebruikersnaam" size="50">
                <p class="labels"><center>Wachtwoord *</center></p><input id="Wachtwoord" type="password" name="Wachtwoord" placeholder="Wachtwoord" size="50">
                <p class="labels"><center>Bevestig wachtwoord *</center></p><input id="Wachtwoord2" type="password" name="Bevestig wachtwoord" placeholder="Bevestig wachtwoord" size="50">
                <br />
                <a href="index.html" style="text-decoration:none"><center><img id="return" name="jsbutton" src="return.png" alt="Terug" /></center></a>
                <br />
                <center><input id="bevestig" type="image" src="Bevestig.png" width="200"  height="50" border="0" alt="SUBMIT!" onclick="formulierValideren()"></center>
                <br />
            </form>
        </div>
</div>
</body>
</html>

Some notes you need to know:

1) since it's an assignment for school, I can't use PHP.

2) The primary key in my database table 'Gebruiker' is 'ID', the field of 'username' is the field which has to be unique

thanks in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Do you have a unique index on gebruiker.username? What do you get back from your AJAX call in this case? – Alain Collins May 09 '13 at 04:56
  • No, gebruiker.username isn't set as unique in the database. I knew that would be a solution, but what does the program do when I give gebruiker.username a unique index? Will it simply do nothing, because the username already exists? or will it throw an error in an alert or something? If not, how do I make this happen? – Kevin Baeyens May 09 '13 at 14:51
  • MySQL won't insert the duplicate row and will throw an error. How your server-side script handles that error is up to it/you. – Alain Collins May 09 '13 at 15:23
  • ok, so I edited my database properties: username has to be unique. I tested if I could insert a duplicate username into the database and it doesn't insert, so that works already. THE QUESTION IS NOW: HOW CAN I SEE WHICH ERROR IS THROWN AND HOW DO I POP UP AN ALERT WITH JAVASCRIPT WITH A MESSAGE THAT THE USERNAME HAS ALREADY BEEN USED, WHEN THIS ERROR IS THROWN? – Kevin Baeyens May 09 '13 at 22:53
  • When you hit the AJAX call with a duplicate user, what do you get back? – Alain Collins May 10 '13 at 16:15
  • This is the error log I get from my Glassfish server when inserting a duplicate user: WARNING: EJB5184:A system exception occurred during an invocation on EJB GebruikerService, method: public javax.ws.rs.core.Response REST.GebruikerService.GebruikerToevoegen(Domein.Gebruiker) WARNING: javax.ejb.EJBException – Kevin Baeyens May 10 '13 at 23:12
  • Caused by: javax.ws.rs.WebApplicationException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'kevin' for key 'NAAM_UNIQUE' – Kevin Baeyens May 10 '13 at 23:13
  • Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'kevin' for key 'NAAM_UNIQUE' – Kevin Baeyens May 10 '13 at 23:13
  • If that message is being sent back from your AJAX call, have your client look for it and behave accordingly. – Alain Collins May 11 '13 at 17:54

0 Answers0