My problem is as follows. I want javascript to check if a value exists in the database. I have a javascript function which checks if an email adress exists in the database. That one is working fine, so i made copy of it and changed it to match a different input field, which should check if a value (woonplaats) exists. Right now, it always shows the message 'plaatsnaam bestaat niet' even though it does exist. The HTML, PHP code with the SQL which checks the database and the javascript are all in the same PHP file. I know the PHP check echo's but i only want the javascript to perform a check.
Underneath my code:
HTML:
<form action="" onSubmit="return !!checkMailStatus() & checkCityStatus();" method="post" id="registration">
<input type="email" name="email" id="email" placeholder="E-mailadres" required/>
<input type="text" name="keyword" id="keyword" placeholder="Woonplaats" value="">
<button type="submit" name="register" class="btn-red">Registreren!</button>
</form>
PHP:
if(isset($_POST['register'])) {
$email = $_POST['email'];
$keyword = $_POST['keyword'];
$check_d = $db->query("SELECT id FROM users WHERE email='".$email."'");
$check_d = $check_d->num_rows;
if($check_d == 1) {
echo 'E-mailadres bestaat al';
}
$check_pnl = ("SELECT woonplaats
FROM `locatie`
WHERE woonplaats='".$keyword."'");
$check_pnl = $check_pnl->num_rows;
if($check_pnl == 0) {
echo 'Plaatsnaam Bestaat Niet';
}
Javascript:
<script>
function checkMailStatus(){
//alert("came");
var email=$("#email").val();
$.ajax({
type:'post',
url:'',
data:{email: email},
success:function(msg){
alert('Gebruik een ander e-mail adres');
return false; //prevent submit from submitting
}
});
}
</script>
<script>
function checkCityStatus(){
//alert("came");
var keyword=$("#keyword").val();
$.ajax({
type:'post',
url:'',
data:{keyword: keyword},
success:function(msg){
alert('Plaatsnaam bestaat niet');
return false; //prevent submit from submitting
}
});
}
</script>
BASED ON THE AWNSERS OF Pioz AND RAJDEEP PAUL I CHANGED THE FOLLOWING:
in index.php I changed the javascript code to:
<script>
var regForm = $("#registration");
regForm.submit(function(evt){
// PREVENT FORM FROM DEFAULT BEHAVIOUR: SUBMITTING...
evt.preventDefault();
// IF BOTH CONDITIONS ARE SATISFIED... SUBMIT THE FORM MANUALLY...
if(checkMailStatus() && checkCityStatus()){
// SHIP THE FORM
$(this).submit();
}
});
function checkMailStatus(){
var email = $("#email").val();
var returnVal = 0;
$.ajax({
type:'post',
url:'check.php',
data:{email: email, checkMail: true},
success:function(msg){
if(msg == "0"){
alert('Gebruik een ander e-mail adres');
}else{
returnVal = 1;
}
}
});
return returnVal;
}
function checkCityStatus(){
var keyword = $("#keyword").val();
var returnVal = 0;
$.ajax({
type:'post',
url:'check.php',
data:{keyword: keyword, checkCity: true},
success:function(msg){
if(msg == "0"){
alert('Plaatsnaam bestaat niet');
}else{
returnVal = 1;
}
}
});
return returnVal;
}
</script>
I ALSO CREATED CHECK.PHP:
if($_POST['checkMail'] == "true"){
$check_d = $db->query("SELECT id FROM users WHERE email='".$email."'");
$check_d = $check_d->num_rows;
if($check_d == 0) {
echo "0";
}else{
// ECHO A FLAG TO SHOW THAT EVERYTHING WORKS FINE: ZERO ISSUE WAS FOUND
echo "1";
}
}
if($_POST['checkCity'] == "true"){
$check_pnl = ("SELECT woonplaats
FROM `locatie`
WHERE woonplaats='".$keyword."'");
$check_pnl = $check_pnl->num_rows;
if($check_pnl == 0) {
echo "1";
}else{
// ECHO A FLAG TO SHOW THAT EVERYTHING WORKS FINE: ZERO ISSUE WAS FOUND
echo "0";
}
}