i'm trying to add data to database with jquery. My code is here, but its not working.
Using bootstrap form, with that form i'm trying to send input datas to specific page (in this situation, adduser.php) in that page, i'm trying to check this values to database for be sure there is no same data (i'm checking email adresses) Can you help me guys?
<script>
$(document).ready(function(){
$('#adduser').click(function(){
var add_name = $('#add_name').val();
var add_surname = $('#add_surname').val();
var add_email = $('#add_email').val();
var add_password = $('#add_password').val();
if(add_name == '' || add_surname == '' || add_email == '' || add_password == '' ){
$('#add_user_error').html("<strong class='text-danger'>*** Please enter all details</strong>");
}else{
$.ajax({
url: "adduser.php",
method: "post",
data:{add_name:add_name,add_surname:add_surname,add_email:add_email,add_password:add_password},
success: function(data){
if (data == 1) {
$('#add_user_error').html("<strong class='text-danger'>This email have in database</strong>");
}else{
$('#add_user_error').html("<strong class='text-success'>Success</strong>");
}
}
}); return false;
}
});
});
</script>
<?php
include ('setup.php'); //Database connection dbc
if(isset($_POST['adduser'])){
$add_name = $_POST['add_name'];
$add_surname = $_POST['add_surname'];
$add_email = $_POST['add_email'];
$add_password = $_POST['password'];
$q = "SELECT * FROM users WHERE email = '$add_email'";
$r = mysqli_query($dbc, $q);
$adduser = mysqli_fetch_assoc($r);
if($adduser['email'] !== $add_email){
$q = "INSERT INTO users (name,surname,email,password) VALUES ('$add_name','$add_surname','$add_email','$add_password')";
$r = mysqli_query($dbc, $q);
}
}
?>