I recently tried to use the PDO classes of php with MSSQL.
I have a form with textboxes and a submit button. Now what I like to do is to do the validation with jQuery to let it go faster. Whenever jQuery is done with the validation and everything is filled it as it should be, it should activate a php function that will add the form as a query with the PDO classes.
Html code:
<div id="registreren">
<div class="titel">
<p class="big"> Registreren| </p>
<p class="small"> Registreer met jouw gewenste gegevens.</p>
</div>
<div id="form">
<form method="POST" action="" onsubmit="return false;">
<table border = '1'>
<tr>
<td>Username:</td>
<td><input type="textbox" name="txtUsername" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="txtPassword" /></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="textbox" name="txtEmail" /></td>
</tr>
<tr>
<td>Telefoonnummer:</td>
<td><input type="textbox" name="txtTel" /></td>
</tr>
<tr>
<td></td>
<td class="showPass"><input type="checkbox" name="showPass" />Laat password zien</td>
</tr>
<tr>
<td><p class="warning4"> <br /> </p></td>
<td><button type="submit" name="registreren" value="registreren" class="btnRegistrate">Registreren</button></td>
</tr>
</table>
</form>
</div>
JQuery code:
var registrateEmpty;
$('.btnRegistrate').click(function() {
$('.warning4').html('');
registrateEmpty = false;
$('#registreren input').each(function() {
if($(this).val() == "")
{
$('.warning4').html('Vul alle velden in!');
registrateEmpty = true;
return false;
}
})
if(registrateEmpty!=true) {
if($('input[name=txtUsername]').val().length < 5 || $('input[name=txtPassword]').val().length < 5) {
$('.warning4').html('Username en password moeten minimaal uit 5 tekens bestaan.');
return false;
}
else if($('input[name=txtTel]').val().match(/[a-zA-Z]/) || $('input[name=txtTel]').val().length != 10) {
$('.warning4').html('Vul een geldige telefoonnummer in.');
return false;
}
else if($('input[name=txtEmail]').val().indexOf('@') == -1 || $('input[name=txtEmail]').val().length < 5) {
$('.warning4').html('Vul een geldige e-mail adres in.');
return false;
}
else {
<?php
toevoegen();
?>
$('.warning4').html('');
}
}
})
What this script actually does do is it will first check if it has any empty input. If everything is filled it, it will check for some specific lenghts of the value that is filled in. When everything is 'good', it will activate the php function
Php function:
include('connect.php');
function toevoegen() {
global $pdo;
try {
$sql = "INSERT INTO account
(acc_Username, acc_Password, acc_Email, acc_Tel, join_Date, privilege)
VALUES
(:username, :password, :email, :tel, :jdate, 1);
";
$s = $pdo -> prepare($sql); //doet hetzelfde als query() alleen moet je hier nog een trigger functie sturen
$s->bindValue(':username', $_POST['txtUsername'], PDO::PARAM_STR);
$s->bindValue(':password', $_POST['txtPassword'], PDO::PARAM_STR);
$s->bindValue(':email', $_POST['txtEmail'], PDO::PARAM_STR);
$s->bindValue(':tel', $_POST['txtTel'], PDO::PARAM_STR);
$s->bindValue(':jdate', date('d-m-Y'), PDO::PARAM_STR);
$s->execute();
$message = "Succeed!";
}
catch (PDOException $e)
{
echo 'Fout bij inserten van een rij: ' . $e->getMessage();
exit;
}
}
What this do is that it will first prepare to sql statement. Then it will fill the open variables from the statement with the textbox values.
Now what does happen is that it seems it executes the php function with out even validating with JQuery. My error :
Undefined index: txtUsername , Undefined index: txtPassword , Undefined index: txtEmail , Undefined index: txtTel
So I hope someone know what went wrong, or how to work with php jquery together like this. I hope that even with some things being other language, it is still understandable. Thanks in advance.