I am trying to communicate with php usring jquery ajax method.
<form class="form-group" id="formm" action="check.php" method="post">
<label for="">Testing</label><br>
<input type="text" class="col-md-5" id="name" placeholder="" name="name"><br>
<button type="button" name='button' id="button" class="btn btn-default" type="submit">button</button>
<p class="help-block" id="result">Help text here.</p>
</form>
my jquery
$(document).ready(function(){
$("#button").click(function(){
$.post("check.php", $( "#formm :input" ).serialize(), function(info) {
$("#result").html(info);
});
});
});
$("#button").click(function(e){
e.preventDefault();
});
my php code
if(isset($_POST['button'])) {
$username = $_POST['name'];
$con = mysqli_connect("localhost","root","","test") or die ("Couldnt connect");
$check = "INSERT INTO name WHERE name='$username'";
$sql_check = mysqli_query($con,$check);
if($sql_check) {
echo "Successfully inserted";
} else {
echo 'Couldnt Insert';
}
}
Now, js script is communicating with php but not as i want it to. 1. when i use serialize(), it stops communicating 2. When i dont use serialize(), my php code sends me back "Couldnt insert" 3. I tried using php errors reporting lines, js doesnt communiate again. No errors on console either. Is there something i am missing?
UPDATE : tried serializing just the formm and not the input element, still not working.