I'm trying to check if a Name already exists in the database. But my Ajax isn't returning the value, honestly not even sure if the Ajax is running at all.
Here's my code on my php page.
<input class="form-control" id="name" name="name" onblur="checkNameStatus()" />
<script>
function checkNameStatus(){
//alert('Start of funtion');
var name=$("#name").val();
//alert("Name Input value is: "+name);
$.ajax({
type:'post',
url:'<?php echo base_url() ?>assets/ajax/checkName.php?name='+name,
success:function(msg){
alert(msg);
}
});
//alert("End of function");
}
</script>
Using alerts I can tell that the function is indeed running and recieving the name input's value.
And then my Ajax
<?php
$hostname = 'localhost';
$username = 'root';
$password = '123';
$dbname = 'test_db';
function clean($string) {
return preg_replace('/[^A-Za-z0-9\- .!@#$%&]/', '', $string);
}
$name= clean($_GET['name']);
$msg = "";
$conn=mysqli_connect($hostname,$username,$password,$dbname);
if (mysqli_connect_errno()){
echo ("Failed to connect to MySQL: " . mysqli_connect_error());
}
$sql = "SELECT * FROM pattern where name = '$name';";
$check=mysqli_query($conn, $sql);
$count=mysqli_num_rows($check);
if($count!=0)
{
$msg = "A Schedule Pattern with the Name of $name already exists. If you continue to use this Name it will overwrite $name's current Data.";
return $msg;
} else {
$msg = "Count check Failed";
echo $msg;
}
mysqli_close($con);
?>
How do I receive the $msg variable from my ajax and return it to the script, and then subsequently show it in an alert?